util: add keyring_describe helper and move to basic
authorLuca Boccassi <bluca@debian.org>
Tue, 4 Jun 2024 12:23:30 +0000 (13:23 +0100)
committerLuca Boccassi <bluca@debian.org>
Thu, 6 Jun 2024 10:39:56 +0000 (11:39 +0100)
So that it can be used from libsystemd. No external dependencies.

src/basic/keyring-util.c [new file with mode: 0644]
src/basic/keyring-util.h [new file with mode: 0644]
src/basic/meson.build
src/libsystemd/sd-id128/sd-id128.c
src/shared/keyring-util.c [deleted file]
src/shared/keyring-util.h [deleted file]
src/shared/meson.build

diff --git a/src/basic/keyring-util.c b/src/basic/keyring-util.c
new file mode 100644 (file)
index 0000000..c32bd50
--- /dev/null
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "keyring-util.h"
+#include "memory-util.h"
+#include "missing_syscall.h"
+
+int keyring_read(key_serial_t serial, void **ret, size_t *ret_size) {
+        size_t bufsize = 100;
+
+        for (;;) {
+                _cleanup_(erase_and_freep) uint8_t *buf = NULL;
+                long n;
+
+                buf = new(uint8_t, bufsize + 1);
+                if (!buf)
+                        return -ENOMEM;
+
+                n = keyctl(KEYCTL_READ, (unsigned long) serial, (unsigned long) buf, (unsigned long) bufsize, 0);
+                if (n < 0)
+                        return -errno;
+
+                if ((size_t) n <= bufsize) {
+                        buf[n] = 0; /* NUL terminate, just in case */
+
+                        if (ret)
+                                *ret = TAKE_PTR(buf);
+                        if (ret_size)
+                                *ret_size = n;
+
+                        return 0;
+                }
+
+                bufsize = (size_t) n;
+        }
+}
+
+int keyring_describe(key_serial_t serial, char **ret) {
+        _cleanup_free_ char *tuple = NULL;
+        size_t sz = 64;
+        int c = -1; /* Workaround for maybe-uninitialized false positive due to missing_syscall indirection */
+
+        assert(ret);
+
+        for (;;) {
+                tuple = new(char, sz);
+                if (!tuple)
+                        return log_oom_debug();
+
+                c = keyctl(KEYCTL_DESCRIBE, serial, (unsigned long) tuple, c, 0);
+                if (c < 0)
+                        return log_debug_errno(errno, "Failed to describe key id %d: %m", serial);
+
+                if ((size_t) c <= sz)
+                        break;
+
+                sz = c;
+                free(tuple);
+        }
+
+        /* The kernel returns a final NUL in the string, verify that. */
+        assert(tuple[c-1] == 0);
+
+        *ret = TAKE_PTR(tuple);
+
+        return 0;
+}
diff --git a/src/basic/keyring-util.h b/src/basic/keyring-util.h
new file mode 100644 (file)
index 0000000..6e6e685
--- /dev/null
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <sys/types.h>
+
+#include "missing_keyctl.h"
+
+/* Like TAKE_PTR() but for key_serial_t, resetting them to -1 */
+#define TAKE_KEY_SERIAL(key_serial) TAKE_GENERIC(key_serial, key_serial_t, -1)
+
+int keyring_read(key_serial_t serial, void **ret, size_t *ret_size);
+int keyring_describe(key_serial_t serial, char **ret);
index d71c9d8bc7921635eb343636fa5639a7cac81030..9a214575a563b2a8e3ed482ef6c8591db490efbb 100644 (file)
@@ -57,6 +57,7 @@ basic_sources = files(
         'lock-util.c',
         'log.c',
         'login-util.c',
+        'keyring-util.c',
         'memfd-util.c',
         'memory-util.c',
         'mempool.c',
index 62b8aaa347dd2b68e7359b13cc1b65c87fad26f0..fc1107b4e8139d500c68b0e5d26d816408e3064b 100644 (file)
@@ -13,6 +13,7 @@
 #include "hmac.h"
 #include "id128-util.h"
 #include "io-util.h"
+#include "keyring-util.h"
 #include "macro.h"
 #include "missing_syscall.h"
 #include "missing_threads.h"
@@ -202,7 +203,6 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
         char *d, *p, *g, *u, *e;
         unsigned long perms;
         key_serial_t key;
-        size_t sz = 256;
         uid_t uid;
         gid_t gid;
         int r, c;
@@ -221,24 +221,9 @@ static int get_invocation_from_keyring(sd_id128_t *ret) {
                 return -errno;
         }
 
-        for (;;) {
-                description = new(char, sz);
-                if (!description)
-                        return -ENOMEM;
-
-                c = keyctl(KEYCTL_DESCRIBE, key, (unsigned long) description, sz, 0);
-                if (c < 0)
-                        return -errno;
-
-                if ((size_t) c <= sz)
-                        break;
-
-                sz = c;
-                free(description);
-        }
-
-        /* The kernel returns a final NUL in the string, verify that. */
-        assert(description[c-1] == 0);
+        r = keyring_describe(key, &description);
+        if (r < 0)
+                return r;
 
         /* Chop off the final description string */
         d = strrchr(description, ';');
diff --git a/src/shared/keyring-util.c b/src/shared/keyring-util.c
deleted file mode 100644 (file)
index fadd90e..0000000
+++ /dev/null
@@ -1,35 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-
-#include "keyring-util.h"
-#include "memory-util.h"
-#include "missing_syscall.h"
-
-int keyring_read(key_serial_t serial, void **ret, size_t *ret_size) {
-        size_t bufsize = 100;
-
-        for (;;) {
-                _cleanup_(erase_and_freep) uint8_t *buf = NULL;
-                long n;
-
-                buf = new(uint8_t, bufsize + 1);
-                if (!buf)
-                        return -ENOMEM;
-
-                n = keyctl(KEYCTL_READ, (unsigned long) serial, (unsigned long) buf, (unsigned long) bufsize, 0);
-                if (n < 0)
-                        return -errno;
-
-                if ((size_t) n <= bufsize) {
-                        buf[n] = 0; /* NUL terminate, just in case */
-
-                        if (ret)
-                                *ret = TAKE_PTR(buf);
-                        if (ret_size)
-                                *ret_size = n;
-
-                        return 0;
-                }
-
-                bufsize = (size_t) n;
-        }
-}
diff --git a/src/shared/keyring-util.h b/src/shared/keyring-util.h
deleted file mode 100644 (file)
index c8c53f1..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#pragma once
-
-#include <sys/types.h>
-
-#include "missing_keyctl.h"
-
-/* Like TAKE_PTR() but for key_serial_t, resetting them to -1 */
-#define TAKE_KEY_SERIAL(key_serial) TAKE_GENERIC(key_serial, key_serial_t, -1)
-
-int keyring_read(key_serial_t serial, void **ret, size_t *ret_size);
index 8fb2b7ec7f6bf125942211a58c103af377098b47..c5106d87d55665e9fa9f91357a1fae4af7bf7e7e 100644 (file)
@@ -100,7 +100,6 @@ shared_sources = files(
         'kbd-util.c',
         'kernel-config.c',
         'kernel-image.c',
-        'keyring-util.c',
         'killall.c',
         'label-util.c',
         'libarchive-util.c',