From patchwork Wed Nov 22 20:43:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 746045 Delivered-To: patch@linaro.org Received: by 2002:a5d:6706:0:b0:32d:baff:b0ca with SMTP id o6csp533484wru; Wed, 22 Nov 2023 12:47:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj6VaZsxFq7DvcMcZfg8h+YF8t59RIC24gJijH+seh5tKyq/EGNvgh+DB5KtkN40dGq+TF X-Received: by 2002:a05:6830:1148:b0:6b8:6f94:d3a2 with SMTP id x8-20020a056830114800b006b86f94d3a2mr3743955otq.25.1700686056446; Wed, 22 Nov 2023 12:47:36 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700686056; cv=pass; d=google.com; s=arc-20160816; b=UKtekGkdN6WfQT14MvdgY5lnT0mvvllgHVIl5hfuWHAbN9njGi3ccsfQ73MdfxENtv 5BO+F+dzLqUGNv/aisWyztX/TzlWLLxb086jpj9SC6mAKBbpNukeZ/Qiq+cav+k43Tn2 spa4/NUZ/+b8krjO1Oo/D1TBbzil+RjKvQSPWJRoZrJ7mJnPxhMT5tYd65Eu/zSjh4Wf BoV74tf3o5rbFVMfdVIoMRqpGrH6jyerOiyIH0QWJ+Wa8DtwGXqubOEY/xuuFouANjI3 xg/7aFEfhNnC0d1gtA7wbyRpcoywzkoS+YI7NZBrNVqKxk317iYHKh+XDbxsJl9y+QeN fLlg== 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:to:from :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=3elD/K7gUXMNYjJWtMK586qrrL88akn0503r8qbMKJA=; fh=Eoh12rOA3G9+1wWUpSCqABE4OFO6OiQ5dhxZkiORJq4=; b=JicSKZfSrZUlCkNsLV3TLHqUA+kkTu/+o6sm44S2LJHmBEoXbiPcdfJtnYTUejitmW nowrz6WrkX6NxBGzWXQZGGC+2oiB7F231wqrel0M9RLAJvfgbqvhuQYuykp8YVuB25Zr JqAQNty5pY1KjfFPNc/CoR8jKhaePgwFOc3Vz/aoLIwoBsELgH7oIUX2j66piwjt4VOP NSKFRJa2LilymqjuTjeynuOIFgaCfh9ajT2rbUgz26uTrfwVF0v3zhLlMyiUaIVZSw+i ZeYODufWaARDv0LFf8Jr0ura9aOdY9vFEl53Y0wVXP5g1hEl8fZQ2T/DfcjbJ0yJ/OOI sJCw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iOiqD4IJ; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t10-20020a05621405ca00b00670bd6ac7e9si217544qvz.208.2023.11.22.12.47.36 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 12:47:36 -0800 (PST) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=iOiqD4IJ; arc=pass (i=1); spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 3303139524A2 for ; Wed, 22 Nov 2023 20:44:21 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x42f.google.com (mail-pf1-x42f.google.com [IPv6:2607:f8b0:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id C0E163861878 for ; Wed, 22 Nov 2023 20:43:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C0E163861878 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 C0E163861878 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700685834; cv=none; b=EhFbCWoefQlbqa87ddGE5EEn/mEDPNiSxMGub7fF4MqRHuzuGYANtpjm9Fqyf7X0Li+wpjA4NbaxQ7tQbq/F+0ZNMTAVIY/u7529cq9aFvdV/sH00hGyh/vzvV/OoTLwhB/kUODqVzDioSSmNy4rNOXmEcBPu8zj8wOCRgY4Nko= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700685834; c=relaxed/simple; bh=kK6HqI5dbES+n2NQ0FsBO61TYQzvmKoupAONYxs4wbQ=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=b34Iwo55VsFzUYogjNmBrjx3hKPw2V+LVGJwC2GThl0PRZCBxxb4inGaJEutrU+UJpNNR3GCFE59dEqouOZDlgWF2E5rygqAPN7UKnu4QJkfxho7CHfi+mls6xPz5y4YSZh4zlAo/LllWkgsRTbEx2OeAsHEzl/HuAAfYjTn0w4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6b7f0170d7bso235096b3a.2 for ; Wed, 22 Nov 2023 12:43:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700685824; x=1701290624; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=3elD/K7gUXMNYjJWtMK586qrrL88akn0503r8qbMKJA=; b=iOiqD4IJGe4QS66XZjYX8wJHKfe0cv2dyucIUtO2hA/IqAKeV4OMJslml+lD9yrfgt 9rOOeXuwrbH+aMxBv374lFnMPIB6cVlu+DvvSIJsZ7/Isl+VjmZD0ynFpBBPlRewXh7C mUMSsiVY/Oon+HN68HUNI3HZgJ9iXU46zEd7wGM/m9GXRcT/SpfuisA/niw15FW38x/q lpFsMAnPug/Gs4i+hBf5C+rFfd2XI3OxoAGM/+TBGrGFHPpWBLBzvPbD+mRkqbKOzN5A 00bnN3gj5Acmjbbnh1K0w7GgzdUzSDZ/NAc7YWFoXQsE2ioimsinLGTM3/51alOxiYHF Fn8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700685824; x=1701290624; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=3elD/K7gUXMNYjJWtMK586qrrL88akn0503r8qbMKJA=; b=WxqNOjhpXfkfaE9EIsRD/einUdKmHCqj8qzYVwatBQjU2NXFpTJ9UV37JGLyFImPf5 ECC8VC5mpjSjAmWORP9L0fIKqcn61Xa3FB3ITgnQY+t3KGn0IDKohEl+0AA0W37tl0UU kjg6vI9aBQkTlIhQYxjg9QbOZ1nk+310ulnGN76nzw7lb5zrbubX/5t4CC7ZHwEz9Cja xOyfAllTxFtwMIwzFLRdTgKs5Rk+pjhZCn3cYcE7e4dOdJUdojXv+bc2i1MV3qJlJEh3 OBn2e09C4MSBLkQNvSTlr91Gw5KLDBgupyNuGP7L0DpkeKgyqRqf/ltsKqFZMYM1l3di qELw== X-Gm-Message-State: AOJu0YxC6+iqcGmSJJN5+re84ghqjH6dzls8Tl5rlNBQAe0OX9g8u5dQ jcOVFctDHF0cRIQucntXrXnnypcb0kHHjBB1FlHBIw== X-Received: by 2002:a05:6a00:3316:b0:6be:e54e:a540 with SMTP id cq22-20020a056a00331600b006bee54ea540mr4631565pfb.30.1700685824143; Wed, 22 Nov 2023 12:43:44 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c2:94e:ee04:b0d3:203c:7e3]) by smtp.gmail.com with ESMTPSA id ei45-20020a056a0080ed00b006cb6119f516sm138389pfb.163.2023.11.22.12.43.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 12:43:43 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, siddhesh@sourceware.org Subject: [PATCH v5 5/5] elf: Refactor process_envvars Date: Wed, 22 Nov 2023 17:43:25 -0300 Message-Id: <20231122204325.4058222-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231122204325.4058222-1-adhemerval.zanella@linaro.org> References: <20231122204325.4058222-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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 It splits between process_envvars_secure and process_envvars_default, with the former used to process arguments for __libc_enable_secure. It does not have any semantic change, just simplify the code so there is no need to handle __libc_enable_secure on each len switch. Checked on x86_64-linux-gnu and aarch64-linux-gnu. Reviewed-by: Siddhesh Poyarekar --- elf/rtld.c | 132 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 84 insertions(+), 48 deletions(-) diff --git a/elf/rtld.c b/elf/rtld.c index cfba30eba0..95dcd32185 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -2527,7 +2527,67 @@ a filename can be specified using the LD_DEBUG_OUTPUT environment variable.\n"); } static void -process_envvars (struct dl_main_state *state) +process_envvars_secure (struct dl_main_state *state) +{ + char **runp = _environ; + char *envline; + + while ((envline = _dl_next_ld_env_entry (&runp)) != NULL) + { + size_t len = 0; + + while (envline[len] != '\0' && envline[len] != '=') + ++len; + + if (envline[len] != '=') + /* This is a "LD_" variable at the end of the string without + a '=' character. Ignore it since otherwise we will access + invalid memory below. */ + continue; + + switch (len) + { + case 5: + /* For __libc_enable_secure mode, audit pathnames containing slashes + are ignored. Also, shared audit objects are only loaded only from + the standard search directories and only if they have set-user-ID + mode bit enabled. */ + if (memcmp (envline, "AUDIT", 5) == 0) + audit_list_add_string (&state->audit_list, &envline[6]); + break; + + case 7: + /* For __libc_enable_secure mode, preload pathnames containing slashes + are ignored. Also, shared objects are only preloaded from the + standard search directories and only if they have set-user-ID mode + bit enabled. */ + if (memcmp (envline, "PRELOAD", 7) == 0) + state->preloadlist = &envline[8]; + break; + } + } + + /* Extra security for SUID binaries. Remove all dangerous environment + variables. */ + const char *nextp = UNSECURE_ENVVARS; + do + { + unsetenv (nextp); + nextp = strchr (nextp, '\0') + 1; + } + while (*nextp != '\0'); + + if (GLRO(dl_debug_mask) != 0 + || GLRO(dl_verbose) != 0 + || GLRO(dl_lazy) != 1 + || GLRO(dl_bind_not) != 0 + || state->mode != rtld_mode_normal + || state->version_info) + _exit (5); +} + +static void +process_envvars_default (struct dl_main_state *state) { char **runp = _environ; char *envline; @@ -2550,15 +2610,13 @@ process_envvars (struct dl_main_state *state) { case 4: /* Warning level, verbose or not. */ - if (!__libc_enable_secure - && memcmp (envline, "WARN", 4) == 0) + if (memcmp (envline, "WARN", 4) == 0) GLRO(dl_verbose) = envline[5] != '\0'; break; case 5: /* Debugging of the dynamic linker? */ - if (!__libc_enable_secure - && memcmp (envline, "DEBUG", 5) == 0) + if (memcmp (envline, "DEBUG", 5) == 0) { process_dl_debug (state, &envline[6]); break; @@ -2573,8 +2631,7 @@ process_envvars (struct dl_main_state *state) case 7: /* Print information about versions. */ - if (!__libc_enable_secure - && memcmp (envline, "VERBOSE", 7) == 0) + if (memcmp (envline, "VERBOSE", 7) == 0) { state->version_info = envline[8] != '\0'; break; @@ -2591,43 +2648,37 @@ process_envvars (struct dl_main_state *state) } /* Which shared object shall be profiled. */ - if (!__libc_enable_secure - && memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0') + if (memcmp (envline, "PROFILE", 7) == 0 && envline[8] != '\0') GLRO(dl_profile) = &envline[8]; break; case 8: /* Do we bind early? */ - if (!__libc_enable_secure - && memcmp (envline, "BIND_NOW", 8) == 0) + if (memcmp (envline, "BIND_NOW", 8) == 0) { GLRO(dl_lazy) = envline[9] == '\0'; break; } - if (! __libc_enable_secure - && memcmp (envline, "BIND_NOT", 8) == 0) + if (memcmp (envline, "BIND_NOT", 8) == 0) GLRO(dl_bind_not) = envline[9] != '\0'; break; case 9: /* Test whether we want to see the content of the auxiliary array passed up from the kernel. */ - if (!__libc_enable_secure - && memcmp (envline, "SHOW_AUXV", 9) == 0) + if (memcmp (envline, "SHOW_AUXV", 9) == 0) _dl_show_auxv (); break; case 11: /* Path where the binary is found. */ - if (!__libc_enable_secure - && memcmp (envline, "ORIGIN_PATH", 11) == 0) + if (memcmp (envline, "ORIGIN_PATH", 11) == 0) GLRO(dl_origin_path) = &envline[12]; break; case 12: /* The library search path. */ - if (!__libc_enable_secure - && memcmp (envline, "LIBRARY_PATH", 12) == 0) + if (memcmp (envline, "LIBRARY_PATH", 12) == 0) { state->library_path = &envline[13]; state->library_path_source = "LD_LIBRARY_PATH"; @@ -2635,30 +2686,26 @@ process_envvars (struct dl_main_state *state) } /* Where to place the profiling data file. */ - if (!__libc_enable_secure - && memcmp (envline, "DEBUG_OUTPUT", 12) == 0) + if (memcmp (envline, "DEBUG_OUTPUT", 12) == 0) { debug_output = &envline[13]; break; } - if (!__libc_enable_secure - && memcmp (envline, "DYNAMIC_WEAK", 12) == 0) + if (memcmp (envline, "DYNAMIC_WEAK", 12) == 0) GLRO(dl_dynamic_weak) = 1; break; case 14: /* Where to place the profiling data file. */ - if (!__libc_enable_secure - && memcmp (envline, "PROFILE_OUTPUT", 14) == 0 + if (memcmp (envline, "PROFILE_OUTPUT", 14) == 0 && envline[15] != '\0') GLRO(dl_profile_output) = &envline[15]; break; case 20: /* The mode of the dynamic linker can be set. */ - if (!__libc_enable_secure - && memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0) + if (memcmp (envline, "TRACE_LOADED_OBJECTS", 20) == 0) { state->mode = rtld_mode_trace; state->mode_trace_program @@ -2668,30 +2715,10 @@ process_envvars (struct dl_main_state *state) } } - /* Extra security for SUID binaries. Remove all dangerous environment - variables. */ - if (__glibc_unlikely (__libc_enable_secure)) - { - const char *nextp = UNSECURE_ENVVARS; - do - { - unsetenv (nextp); - nextp = strchr (nextp, '\0') + 1; - } - while (*nextp != '\0'); - - if (GLRO(dl_debug_mask) != 0 - || GLRO(dl_verbose) != 0 - || GLRO(dl_lazy) != 1 - || GLRO(dl_bind_not) != 0 - || state->mode != rtld_mode_normal - || state->version_info) - _exit (5); - } /* If we have to run the dynamic linker in debugging mode and the LD_DEBUG_OUTPUT environment variable is given, we write the debug messages to this file. */ - else if (GLRO(dl_debug_mask) != 0 && debug_output != NULL) + if (GLRO(dl_debug_mask) != 0 && debug_output != NULL) { const int flags = O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW; size_t name_len = strlen (debug_output); @@ -2710,6 +2737,15 @@ process_envvars (struct dl_main_state *state) } } +static void +process_envvars (struct dl_main_state *state) +{ + if (__glibc_unlikely (__libc_enable_secure)) + process_envvars_secure (state); + else + process_envvars_default (state); +} + #if HP_TIMING_INLINE static void print_statistics_item (const char *title, hp_timing_t time,