aboutgitcodelistschat:MatrixIRC
path: root/common
diff options
context:
space:
mode:
authorAlice Frosi <afrosi@redhat.com>2023-05-10 11:06:12 +0200
committerAlice Frosi <afrosi@redhat.com>2023-05-10 12:18:57 +0200
commit92afac2a0ca640f19d39da6e7e82e1acb93e2024 (patch)
tree52e0acb81db84833b76d36128800b26dab86effa /common
parent0977f0876af186975d3861c53b8431a80a27fa83 (diff)
downloadseitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar.gz
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar.bz2
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar.lz
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar.xz
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.tar.zst
seitan-92afac2a0ca640f19d39da6e7e82e1acb93e2024.zip
Refactoring of gluten_read/write
Refactor includes: - use static inline instead of macro - return -1 if there is an error and don't exit - eval return 0 or -1 - adjust code and tests
Diffstat (limited to 'common')
-rw-r--r--common/gluten.h60
1 files changed, 34 insertions, 26 deletions
diff --git a/common/gluten.h b/common/gluten.h
index d1d3c61..078c6fa 100644
--- a/common/gluten.h
+++ b/common/gluten.h
@@ -12,6 +12,8 @@
#include <stdbool.h>
#include <linux/seccomp.h>
+#include <stdio.h>
+
#include "util.h"
extern struct seccomp_data anonymous_seccomp_data;
@@ -26,30 +28,6 @@ extern struct seccomp_data anonymous_seccomp_data;
MAX(MAX(MAX(DATA_SIZE, RO_DATA_SIZE), INST_MAX), \
ARRAY_SIZE(anonymous_seccomp_data.args))
-#define check_gluten_limits(g, v, size) \
- do { \
- struct gluten_offset off = { v.type, v.offset + (size) }; \
- if (!is_offset_valid(off)) \
- die(" invalid offset: %d", off.offset); \
- } while (0)
-
-#define gluten_write(g, dst, src) \
- do { \
- void *p = gluten_write_ptr((g), (dst)); \
- check_gluten_limits((g), (dst), sizeof((src))); \
- if (p == NULL) \
- die(" invalid type of offset"); \
- memcpy(p, &(src), sizeof(src)); \
- } while (0)
-
-#define gluten_read(s, g, dst, src, size) \
- do { \
- const void *p = gluten_ptr((s), (g), (src)); \
- check_gluten_limits((g), (src), (size)); \
- if (p == NULL) \
- die(" invalid type of offset"); \
- memcpy(&(dst), p, (size)); \
- } while (0)
enum gluten_offset_type {
OFFSET_RO_DATA = 0,
@@ -242,7 +220,7 @@ static inline void *gluten_write_ptr(struct gluten *g,
#endif
{
if (!is_offset_valid(x))
- die(" invalid offset: %d", x.offset);
+ return NULL;
switch (x.type) {
case OFFSET_DATA:
@@ -264,7 +242,7 @@ static inline const void *gluten_ptr(const struct seccomp_data *s,
const struct gluten_offset x)
{
if (!is_offset_valid(x))
- die(" invalid offset: %d", x.offset);
+ return NULL;
switch (x.type) {
case OFFSET_DATA:
@@ -279,5 +257,35 @@ static inline const void *gluten_ptr(const struct seccomp_data *s,
return NULL;
}
}
+
+static inline bool check_gluten_limits(struct gluten_offset v, size_t size)
+{
+ struct gluten_offset off = { v.type, v.offset + size };
+ return is_offset_valid(off);
+}
+
+static inline int gluten_write(struct gluten *g, struct gluten_offset dst,
+ const void *src, size_t size)
+{
+ void *p = gluten_write_ptr(g, dst);
+ if (p == NULL || !check_gluten_limits(dst, size))
+ return -1;
+ memcpy(p, src, size);
+
+ return 0;
+}
+
+static inline int gluten_read(const struct seccomp_data *s, struct gluten *g,
+ void *dst, const struct gluten_offset src,
+ size_t size)
+{
+ const void *p = gluten_ptr(s, g, src);
+ if (p == NULL || !check_gluten_limits(src, size))
+ return -1;
+ memcpy(dst, p, size);
+
+ return 0;
+}
+
#endif
#endif /* COMMON_GLUTEN_H */