]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
libsystemd/sd-id128: use only internal hmac, remove khash/OpenSSL support
authorLuca Boccassi <luca.boccassi@microsoft.com>
Thu, 30 Sep 2021 10:01:45 +0000 (11:01 +0100)
committerLuca Boccassi <luca.boccassi@microsoft.com>
Fri, 8 Oct 2021 12:11:00 +0000 (13:11 +0100)
Using OpenSSL brings in an additional dependency for all users of
libsystemd.so even though it's just one API that makes use of it.

The khash implementation is awkward as it requires context switches and
computation inside the kernel, thus leaving the process.

Remove both from libsystemd.so, and use exclusively the internal hmac fallback.
While this is not optimized, the sd-id128 API is not used in
performance-critical contexts where hardware acceleration would make a
noticeable difference.

LICENSES/README.md
src/libsystemd/meson.build
src/libsystemd/sd-id128/sd-id128.c
src/test/test-id128.c

index f01049c13f1237e78e650f7c7eab490c91349868..3c28de51b1805845b51668a23e47572a98caa881 100644 (file)
@@ -58,3 +58,8 @@ The following exceptions apply:
    **BSD-3-Clause** license.
  * any files under test/ without an explicit license we assume non-copyrightable
    (eg: computer-generated fuzzer data)
+
+## OpenSSL Notes
+
+Note that building the systemd project with OpenSSL does not affect the libsystemd.so
+shared library, which is not linked with the OpenSSL library.
index 8ec871f6a445ab6d8d65600d462112f10dee8d05..02b2cd64b2b3e5e43f07d57da9d9ad854a5bfcb9 100644 (file)
@@ -170,8 +170,7 @@ libsystemd_static = static_library(
         include_directories : libsystemd_includes,
         link_with : libbasic,
         dependencies : [threads,
-                        librt,
-                        libopenssl],
+                        librt],
         c_args : libsystemd_c_args)
 
 libsystemd_sym = files('libsystemd.sym')
index 28ae10a19867f82100fbdbc87cfc114d1a23dd5a..992b19130e8cc92419058cf92762f4071fc7dba0 100644 (file)
@@ -4,21 +4,14 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-#if HAVE_OPENSSL
-#include <openssl/hmac.h>
-#include <openssl/sha.h>
-#endif
-
 #include "sd-id128.h"
 
 #include "alloc-util.h"
 #include "fd-util.h"
 #include "hexdecoct.h"
+#include "hmac.h"
 #include "id128-util.h"
 #include "io-util.h"
-#if !HAVE_OPENSSL
-#include "khash.h"
-#endif
 #include "macro.h"
 #include "missing_syscall.h"
 #include "random-util.h"
@@ -278,43 +271,15 @@ _public_ int sd_id128_randomize(sd_id128_t *ret) {
 }
 
 static int get_app_specific(sd_id128_t base, sd_id128_t app_id, sd_id128_t *ret) {
+        uint8_t hmac[SHA256_DIGEST_SIZE];
         sd_id128_t result;
 
         assert(ret);
 
-#if HAVE_OPENSSL
-        /* We prefer doing this in-process, since we this means we are not dependent on kernel configuration,
-         * and this also works in locked down container environments. But some distros don't like OpenSSL's
-         * license and its (in-) compatibility with GPL2, hence also support khash */
-        uint8_t md[256/8];
-        if (!HMAC(EVP_sha256(),
-                  &base, sizeof(base),
-                  (const unsigned char*) &app_id, sizeof(app_id),
-                  md, NULL))
-                return -ENOTRECOVERABLE;
+        hmac_sha256(&base, sizeof(base), &app_id, sizeof(app_id), hmac);
 
         /* Take only the first half. */
-        memcpy(&result, md, MIN(sizeof(md), sizeof(result)));
-#else
-        _cleanup_(khash_unrefp) khash *h = NULL;
-        const void *p;
-        int r;
-
-        r = khash_new_with_key(&h, "hmac(sha256)", &base, sizeof(base));
-        if (r < 0)
-                return r;
-
-        r = khash_put(h, &app_id, sizeof(app_id));
-        if (r < 0)
-                return r;
-
-        r = khash_digest_data(h, &p);
-        if (r < 0)
-                return r;
-
-        /* We chop off the trailing 16 bytes */
-        memcpy(&result, p, MIN(khash_get_size(h), sizeof(result)));
-#endif
+        memcpy(&result, hmac, MIN(sizeof(hmac), sizeof(result)));
 
         *ret = id128_make_v4_uuid(result);
         return 0;
index a61b35b9a3367a9fd071463fb655f19bc83e80ed..55fdab0ab8900e8bbc27b9ec9b005cebcbeb4efc 100644 (file)
@@ -146,16 +146,11 @@ int main(int argc, char *argv[]) {
         assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
         assert_se(sd_id128_equal(id, id2));
 
-        r = sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id);
-        if (r == -EOPNOTSUPP)
-                log_info("khash not supported on this kernel, skipping sd_id128_get_machine_app_specific() checks");
-        else {
-                assert_se(r >= 0);
-                assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
-                assert_se(sd_id128_equal(id, id2));
-                assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
-                assert_se(!sd_id128_equal(id, id2));
-        }
+        assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id) >= 0);
+        assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
+        assert_se(sd_id128_equal(id, id2));
+        assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
+        assert_se(!sd_id128_equal(id, id2));
 
         /* Query the invocation ID */
         r = sd_id128_get_invocation(&id);