From patchwork Mon May 6 16:18:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 795025 Delivered-To: patch@linaro.org Received: by 2002:adf:a453:0:b0:34e:ceec:bfcd with SMTP id e19csp1007836wra; Mon, 6 May 2024 09:22:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXYkodKRcLBksATDrfVK5nw+n+Xgj6U8TLC2Ms0oIVTUwgfvdE/kqaVC295DUsNLF2wd4v2RAFohQsxDTHs2BmO X-Google-Smtp-Source: AGHT+IHViYDgOVJA9yX1lBlc1HhZ3B3RhbQK5SybujjkT+5liqwHCAMEupIcIN5dUZCnGIsUytZz X-Received: by 2002:a05:6122:31a8:b0:4df:2137:e929 with SMTP id ch40-20020a05612231a800b004df2137e929mr8722043vkb.15.1715012558988; Mon, 06 May 2024 09:22:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1715012558; cv=pass; d=google.com; s=arc-20160816; b=pglmfNqf2ujF7ZjHBvWgP6UD5cNkXHc6sk4eF8kZRSiGUNE2wzIYOSnt+0mmMCgRIr YJHXc5uJPMSbKoqWmW5nfK86zJj897Vup2ikBxvJpUY0YzvKBHeqaY2rDi887P/86GYP DYASmuM2+Jj6eKsf0ga2kiCymRiUUP0hS/KXid/Xb5rwvAzQv4Rx9BrwQuVSheYh3UkY U4ngz5MxqTUxBkVD/pqbEkhl90vuQu3gEX/YbtPs3CN91hBrDli9jzJ/pkMBCbNCMJwo 7/6Pw5XOxy44apqthNGUPKbI/wOz9Bt7//JALE5uHivPNrxDV/Zpis34m20fuWiF6pPg S8cQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=qoiKmHdX0UZ9a/7145q2mKR55bpO592xOlaygD0jzrM=; fh=Fj4D3X8lmeg8caKyS1psvSc6iiuGGH95h/ZziX55t10=; b=Pg6BhSlfrtk5RqXJUfqkipv/AlCp5yGIl7vbCKu4oP/YFzC+1WNixy5H5BPFtJUQ2B HOazdawygi1NuVuqbp/Tzqrrd+yGizWCW5BBhTq0DfpjD4DRbkf99lfsglmAhdLXCZKf 7xq1CV3ql5hTGjXQdFylIFgBONl7afiFkId1O6jH8aDR8qHc1q5Gg7rJijtntQ6Ug9Hz RdpaHgTxracQGM1eTU3rTGPeGXYVUQyPORNLA+urwLDMFa7O8btQNelLtHP09+5ka1x/ Ulp7r7wb9fsGz+YUd6QrEmvjXocXh5AmiUqXzYSnVMpVY1ZNI5masdX9LC/kKTsTDFnT xycg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ULUEcJic; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id k4-20020ac5cce4000000b004b9c891e4absi1627183vkn.157.2024.05.06.09.22.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 09:22:38 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ULUEcJic; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3553B3858D35 for ; Mon, 6 May 2024 16:22:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 7DE743858C62 for ; Mon, 6 May 2024 16:20:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7DE743858C62 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 7DE743858C62 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715012416; cv=none; b=QQcu8Io4DhYiGbmSMXw1sfo1OKOZtE/XE6+xn1/4B051MfiUCekw0aPIcTFkXJAYrFHvVN5+WZCelmwV/3okL4GqvNz6aGNhSXwNpLcapKBIuX7oIfk+byqLObk7Ogp8s/oUXor5I91orH3NjT0xqVbhn8DMMeKKWPEd+fJp0fs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715012416; c=relaxed/simple; bh=Bx77GcPaoV/PKQ0ks4wegqXSG/Uhi2kZPuxk7dT6cZ0=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uWmIKtyK++UYKjU1gwoeln72zcLXPKx0qrSPTSffThDuSpByEdYbVLPTRrSMkmA1l/YNCxfhhM+30CO32jTKMNnJlGO9q8Ex8EGUGmJcAs4PHhKMxoWUc7ZZhTZtlGWDmGrU06mCLCk909cTeqfYn4khdrJJs+h0t8TdRgDzi4o= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6f44ed6e82fso1999868b3a.3 for ; Mon, 06 May 2024 09:20:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1715012413; x=1715617213; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=qoiKmHdX0UZ9a/7145q2mKR55bpO592xOlaygD0jzrM=; b=ULUEcJicJrgl8ponBKjQ1oZU80WF0wiytdJlN+UhSfMHporrRqInIoWKgzk3I3NiBx dVs3rIRQe3N+81t2lHYwdSpYvLnKdqNmZgty85oPtRQF0hur2bvnTPIaUYPjzGyIaaCg hscSvzijdWiAp7YfBgnOGu4mNL6tJdLjtWUa+TN4WKFMtNAB2Mpa1exdhSpscylJfgmO ICHNNfb09rpisO/THlyI1zDh8nfVjX+D4KFDMAu9ANUU6vImOch/SNEPOjyE6muFSNPn Csdt5sMBEA8fKw/RQPUUKAyS4BoDy0OOI2D8i7NHUxsx2YrKIFlp2Q6yAE9EgpfK4s2e CMlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715012413; x=1715617213; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=qoiKmHdX0UZ9a/7145q2mKR55bpO592xOlaygD0jzrM=; b=itZ9wry8BampLSwGjTa2kv7LGaAYGpETKdiugmPeXGQqyCuXV9W9kC7rz56nwK9gGh IVknci61xmxwfE6Md+U1jcw9MuXhEd0QNcxMUp4yXuxNpRYCKS/prYjFtqINzXGWTA4b 0qUoxfkHH3A77rlFgh4eM+HCstDc8L/VqjHr28s4757xA4R27RDzocDUOov8dxwHL5E5 mR8zBA5ahY32JzamagRhT97ogHhECmXiwA3WneH6YvDuzHw/32G9uTMZzUdiNCS0/rDy lnOoWDak+wxpxL4NrIQL4Ehh1Yyu+BcqL+rbD8aS9XTa2fHIbSuxx0HmXelQcwKNCfmD A/NQ== X-Gm-Message-State: AOJu0Ywh8jZmq278pVrK/EFPkNepsLnp55pdMT0BzNfLJNXFd8bc5SWi zwxqBULrbFBqfoGZ+bLXKJdDfS0VJtokMin7ALPoU/3r5HEJ/0ELqBIBsY5Hmx9eJAjAYxmX91M CF+Y= X-Received: by 2002:a05:6a00:2287:b0:6f3:ee4f:e28a with SMTP id f7-20020a056a00228700b006f3ee4fe28amr11953302pfe.32.1715012412854; Mon, 06 May 2024 09:20:12 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c2:6e56:fc45:bb45:8b35:9b81]) by smtp.gmail.com with ESMTPSA id ll12-20020a056a00728c00b006ed0b798f1fsm7883347pfb.119.2024.05.06.09.20.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 09:20:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Joe Simmons-Talbott , Siddhesh Poyarekar Subject: [PATCH v3 4/4] elf: Make glibc.rtld.enable_secure ignore alias environment variables Date: Mon, 6 May 2024 13:18:48 -0300 Message-ID: <20240506161955.1570278-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240506161955.1570278-1-adhemerval.zanella@linaro.org> References: <20240506161955.1570278-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patch=linaro.org@sourceware.org Tunable with environment variables aliases are also ignored if glibc.rtld.enable_secure is enabled. The tunable parsing is also optimized a bit, where the loop that checks each environment variable only checks for the tunables with aliases instead of all tables. Checked on aarch64-linux-gnu and x86_64-linux-gnu. Reviewed-by: Siddhesh Poyarekar --- elf/dl-tunables.c | 36 ++++++-- elf/tst-tunables-enable_secure.c | 136 +++++++++++++++++++++++++++---- scripts/gen-tunables.awk | 16 +++- 3 files changed, 164 insertions(+), 24 deletions(-) diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 63cf8c7ab5..147cc4cf23 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -300,6 +300,9 @@ __tunables_init (char **envp) if (__libc_enable_secure) return; + enum { tunable_num_env_alias = array_length (tunable_env_alias_list) }; + struct tunable_toset_t tunables_env_alias[tunable_num_env_alias] = { 0 }; + while ((envp = get_next_env (envp, &envname, &envval, &prev_envp)) != NULL) { /* The environment variable is allocated on the stack by the kernel, so @@ -311,29 +314,44 @@ __tunables_init (char **envp) continue; } - for (int i = 0; i < tunables_list_size; i++) + for (int i = 0; i < tunable_num_env_alias; i++) { - tunable_t *cur = &tunable_list[i]; + tunable_t *cur = &tunable_list[tunable_env_alias_list[i]]; + const char *name = cur->env_alias; - /* Skip over tunables that have either been set already or should be - skipped. */ - if (cur->initialized || cur->env_alias[0] == '\0') + if (name[0] == '\0') continue; - const char *name = cur->env_alias; - - /* We have a match. Initialize and move on to the next line. */ if (tunable_is_name (name, envname)) { size_t envvallen = 0; /* The environment variable is always null-terminated. */ for (const char *p = envval; *p != '\0'; p++, envvallen++); - tunable_initialize (cur, envval, envvallen); + tunables_env_alias[i] = + (struct tunable_toset_t) { cur, envval, envvallen }; break; } } } + + /* Check if glibc.rtld.enable_secure was set and skip over the environment + variables aliases. */ + if (__libc_enable_secure) + return; + + for (int i = 0; i < tunable_num_env_alias; i++) + { + /* Skip over tunables that have either been set or already initialized. */ + if (tunables_env_alias[i].t == NULL + || tunables_env_alias[i].t->initialized) + continue; + + if (!tunable_initialize (tunables_env_alias[i].t, + tunables_env_alias[i].value, + tunables_env_alias[i].len)) + parse_tunable_print_error (&tunables_env_alias[i]); + } } void diff --git a/elf/tst-tunables-enable_secure.c b/elf/tst-tunables-enable_secure.c index f5db1c84e9..99a065e8db 100644 --- a/elf/tst-tunables-enable_secure.c +++ b/elf/tst-tunables-enable_secure.c @@ -17,6 +17,10 @@ . */ #include +/* The test uses the tunable_env_alias_list size, which is only exported for + ld.so. This will result in a copy of tunable_list and + tunable_env_alias_list, which is ununsed by the test itself. */ +#define TUNABLES_INTERNAL 1 #include #include #include @@ -34,6 +38,8 @@ static int restart; static const struct test_t { const char *env; + const char *extraenv; + bool check_multiple; int32_t expected_malloc_check; int32_t expected_enable_secure; } tests[] = @@ -41,39 +47,124 @@ static const struct test_t /* Expected tunable format. */ /* Tunables should be ignored if enable_secure is set. */ { - "glibc.malloc.check=2:glibc.rtld.enable_secure=1", + "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1", + NULL, + false, 0, 1, }, /* Tunables should be ignored if enable_secure is set. */ { - "glibc.rtld.enable_secure=1:glibc.malloc.check=2", + "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2", + NULL, + false, 0, 1, }, /* Tunables should be set if enable_secure is unset. */ { - "glibc.rtld.enable_secure=0:glibc.malloc.check=2", + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2", + NULL, + false, 2, 0, }, + /* Tunables should be ignored if enable_secure is set. */ + { + "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1", + "MALLOC_CHECK_=2", + false, + 0, + 1, + }, + /* Same as before, but with enviroment alias prior GLIBC_TUNABLES. */ + { + "MALLOC_CHECK_=2", + "GLIBC_TUNABLES=glibc.malloc.check=2:glibc.rtld.enable_secure=1", + false, + 0, + 1, + }, + /* Tunables should be ignored if enable_secure is set. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2", + "MALLOC_CHECK_=2", + false, + 0, + 1, + }, + { + "MALLOC_CHECK_=2", + "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2", + false, + 0, + 1, + }, + /* Tunables should be set if enable_secure is unset. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2", + /* Tunable have precedence over the environment variable. */ + "MALLOC_CHECK_=1", + false, + 2, + 0, + }, + { + "MALLOC_CHECK_=1", + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0:glibc.malloc.check=2", + /* Tunable have precedence over the environment variable. */ + false, + 2, + 0, + }, + /* Tunables should be set if enable_secure is unset. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0", + /* Tunable have precedence over the environment variable. */ + "MALLOC_CHECK_=1", + false, + 1, + 0, + }, + /* Tunables should be set if enable_secure is unset. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0", + /* Tunable have precedence over the environment variable. */ + "MALLOC_CHECK_=1", + false, + 1, + 0, + }, + /* Check with tunables environment variable alias set multiple times. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=1:glibc.malloc.check=2", + "MALLOC_CHECK_=2", + true, + 0, + 1, + }, + /* Tunables should be set if enable_secure is unset. */ + { + "GLIBC_TUNABLES=glibc.rtld.enable_secure=0", + /* Tunable have precedence over the environment variable. */ + "MALLOC_CHECK_=1", + true, + 1, + 0, + }, }; static int handle_restart (int i) { if (tests[i].expected_enable_secure == 1) - { - TEST_COMPARE (1, __libc_enable_secure); - } + TEST_COMPARE (1, __libc_enable_secure); else - { - TEST_COMPARE (tests[i].expected_malloc_check, - TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL)); - TEST_COMPARE (tests[i].expected_enable_secure, - TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t, - NULL)); - } + TEST_COMPARE (tests[i].expected_enable_secure, + TUNABLE_GET_FULL (glibc, rtld, enable_secure, int32_t, + NULL)); + TEST_COMPARE (tests[i].expected_malloc_check, + TUNABLE_GET_FULL (glibc, malloc, check, int32_t, NULL)); return 0; } @@ -106,14 +197,31 @@ do_test (int argc, char *argv[]) spargv[i] = NULL; } + enum { tunable_num_env_alias = array_length (tunable_env_alias_list) }; + for (int i = 0; i < array_length (tests); i++) { snprintf (nteststr, sizeof nteststr, "%d", i); printf ("[%d] Spawned test for %s\n", i, tests[i].env); setenv ("GLIBC_TUNABLES", tests[i].env, 1); + + char *envp[2 + tunable_num_env_alias + 1] = + { + (char *) tests[i].env, + (char *) tests[i].extraenv, + NULL, + }; + if (tests[i].check_multiple) + { + int j; + for (j=0; j < tunable_num_env_alias; j++) + envp[j + 2] = (char *) tests[i].extraenv; + envp[j + 2] = NULL; + } + struct support_capture_subprocess result - = support_capture_subprogram (spargv[0], spargv, NULL); + = support_capture_subprogram (spargv[0], spargv, envp); support_capture_subprocess_check (&result, "tst-tunables-enable_secure", 0, sc_allow_stderr); support_capture_subprocess_free (&result); diff --git a/scripts/gen-tunables.awk b/scripts/gen-tunables.awk index 9f5336381e..fc3b41376f 100644 --- a/scripts/gen-tunables.awk +++ b/scripts/gen-tunables.awk @@ -156,7 +156,7 @@ END { print "# define TUNABLE_ALIAS_MAX " (max_alias_len + 1) print "# include \"dl-tunable-types.h\"" # Finally, the tunable list. - print "static tunable_t tunable_list[] attribute_relro = {" + print "static tunable_t tunable_list[] attribute_relro __attribute_used__ = {" for (tnm in types) { split (tnm, indices, SUBSEP); t = indices[1]; @@ -168,5 +168,19 @@ END { default_val[t,n,m], env_alias[t,n,m]); } print "};" + + # Map of tunable with environment variables aliases used during parsing. */ + print "\nstatic const tunable_id_t tunable_env_alias_list[] =" + printf "{\n" + for (tnm in types) { + split (tnm, indices, SUBSEP); + t = indices[1]; + n = indices[2]; + m = indices[3]; + if (env_alias[t,n,m] != "{0}") { + printf (" TUNABLE_ENUM_NAME(%s, %s, %s),\n", t, n, m); + } + } + printf "};\n" print "#endif" }