Welcome to little lamb

Code » limb » commit c5e94fc

shldata-rw: Add shldata_chkmagic()

author Olivier Brunel
2023-05-04 19:24:36 UTC
committer Olivier Brunel
2023-05-20 18:06:40 UTC
parent 1d089d036a05653289b372cd34f6a026c4db1a72

shldata-rw: Add shldata_chkmagic()

src/liblimb/include/limb/shldata-rw.h +5 -0
src/liblimb/shldata-rw.h/shldata_chkmagic.c +11 -0
src/liblimb/shldata-rw.h/shldata_read.c +1 -0
src/liblimb/shldata-rw.h/shldata_write.c +3 -0

diff --git a/src/liblimb/include/limb/shldata-rw.h b/src/liblimb/include/limb/shldata-rw.h
index 7c81454..77843c3 100644
--- a/src/liblimb/include/limb/shldata-rw.h
+++ b/src/liblimb/include/limb/shldata-rw.h
@@ -8,6 +8,11 @@
 #include <limb/int.h>
 #include <limb/siovec.h>
 
+#define SHLDATA_MASK        0xf0f0f800
+#define SHLDATA_MAGIC       0xa0e0f800
+
+extern int shldata_chkmagic(u32 magic);
+
 extern int shldata_write(int bfd, const char *file, u32 magic, u64 ver,
                          const char *pwd, size_t plen, unsigned algo, unsigned iter,
                          int inplace, const struct iovec v[], unsigned n);
diff --git a/src/liblimb/shldata-rw.h/shldata_chkmagic.c b/src/liblimb/shldata-rw.h/shldata_chkmagic.c
new file mode 100644
index 0000000..fac1c2a
--- /dev/null
+++ b/src/liblimb/shldata-rw.h/shldata_chkmagic.c
@@ -0,0 +1,11 @@
+/* This file is part of limb                           https://lila.oss/limb
+ * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
+/* SPDX-License-Identifier: GPL-2.0-only */
+#include <errno.h>
+#include <limb/shldata-rw.h>
+
+int
+shldata_chkmagic(u32 magic)
+{
+    return ((magic & SHLDATA_MASK) == SHLDATA_MAGIC) ? 1 : (errno = EINVAL, 0);
+}
diff --git a/src/liblimb/shldata-rw.h/shldata_read.c b/src/liblimb/shldata-rw.h/shldata_read.c
index 26982c0..d634ec1 100644
--- a/src/liblimb/shldata-rw.h/shldata_read.c
+++ b/src/liblimb/shldata-rw.h/shldata_read.c
@@ -22,6 +22,7 @@ shldata_read(u32 *magic, u64 *ver, stralloc *sa, int bfd, const char *file,
 
     size_t salen = sa->len;
     if (!buffer_gethdr(&b, magic, ver)
+            || !shldata_chkmagic(*magic)
             || !buffer_shldata_getinit_sa(&b, pwd, plen, sa, &bsd)
             || !stralloc_readyplus(sa, buffer_shldata_datasize(&bsd))
             || !buffer_shldata_get(&b, sa->s + sa->len, buffer_shldata_datasize(&bsd), &bsd)
diff --git a/src/liblimb/shldata-rw.h/shldata_write.c b/src/liblimb/shldata-rw.h/shldata_write.c
index c146853..11f2093 100644
--- a/src/liblimb/shldata-rw.h/shldata_write.c
+++ b/src/liblimb/shldata-rw.h/shldata_write.c
@@ -22,6 +22,9 @@ shldata_write(int bfd, const char *file, u32 magic, u64 ver,
     buffer b = BUFFER_INIT(&buffer_write, -1, buf, sizeof(buf));
     char tmp[] = ".tmp.shldata.XXXXXX";
 
+    if (!shldata_chkmagic(magic))
+        return 0;
+
     b.fd = mkfiletempat(bfd, tmp, (createat_fn) (void (*)(void)) open_exclat, 0, NULL);
     if (b.fd < 0) return 0;