From patchwork Wed Nov 22 20:43:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 746043 Delivered-To: patch@linaro.org Received: by 2002:a5d:6706:0:b0:32d:baff:b0ca with SMTP id o6csp532578wru; Wed, 22 Nov 2023 12:45:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGrgjFGmxP6jUM8PqJtIsnSquSJKj4InG0WKW1zeBpWWmVTrVZZTAxm+fApaM4thqcgK0bL X-Received: by 2002:a05:6102:3:b0:462:8841:feb2 with SMTP id j3-20020a056102000300b004628841feb2mr3874251vsp.4.1700685910073; Wed, 22 Nov 2023 12:45:10 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700685910; cv=pass; d=google.com; s=arc-20160816; b=Lf50ynJnD0suCa7eIeP044U8ssa5FS9bQhLRMqIvCBMFCUfEMlnC1IRMNghb+EatbG RlNAzyVqQsJwVnNjMgRLqlpaHXhUSc8qg77Uan9XbcNxV7Jozcp+6EyEOYQ4MrLTGJnk rdcR3HPfWjvVdaUK9Xra89iUVgoue/peOQiKs1wIpx2MWmt6P2wCY/pAbZN050BxcoCF JdHhTGoVyUvU1q2QNLYW+LjudBRDoZyOVOOZTv5DHIhgNOgoQj8z88826FcBOkgAiu0Z 2VsfoICNY7/JA0vX8dopye0lJey1WZRwO+JemBmC++k0I21RFYlDDWRCxmwfJLSyoj2E sCAw== 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=pUWcj3BzUmRCryjJQvH1wKXPtoswVG7ctsf2+rf7hG0=; fh=Eoh12rOA3G9+1wWUpSCqABE4OFO6OiQ5dhxZkiORJq4=; b=OSwA6RHsro0mtJKdzuGJATLBLCCgxPuYs6xlSOfPMypHyElSkS259kom1835d0WpTg 7XXjwncvk5xdsIdlhb/oy51zx373sImArxK0TYkxeD6ND1zZ2ltqKo/EKZzh7ZU0bGr/ AyPpEfnXtOx+jhrUBTJxzycnFn3SI6L3KgnoOkc3xgUMnpd5gavm5ByJYg4Fczbv15Jj njSaLlFJiN4GI7PDTpoL9Eo3yTPv4PfduQS7FfXPVUjkOmxbQEad/A4fBGsgZn1X6H+C V+VJHumdHhKoj1ozE7XNxzwik8qKH+Tr5R01gUFn5DiIhwb9eN5KHuxnc+uMezRrEEZ/ xAPQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VisRCQ2o; 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 dn3-20020a056214094300b0067803c1d6dcsi229768qvb.11.2023.11.22.12.45.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 12:45:10 -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=VisRCQ2o; 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 220853895FF7 for ; Wed, 22 Nov 2023 20:43:56 +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 5AF223858290 for ; Wed, 22 Nov 2023 20:43:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AF223858290 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 5AF223858290 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=1700685831; cv=none; b=YD+wQNk/olo5r56up6HZbbhg/Ya7vxTVu+C1qVTd0jM4P3VQoKy0quSBbB0W1SxWtI7XQdmOKjGITPgUCeJ9edNfSkmQcFxTqga/uyqZHsCyrWibMSGnxFfvoa3oKbWY+mAeathnp48WOy0hbMHVue0p90Zx4dhxHhx3lPZVYEg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700685831; c=relaxed/simple; bh=d3QqfTKU70HN98KTOVOiwT+p8Xg8i0272M2fMzdVGW8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Ct8uhsRS1I/05+NMC727aSHUtfeyRONE9/r5Ys3a/clVaeZUvj0jp+P5drU9z3p89wYNev2TVxcfRpH1x1uNQCS6Aw0p8jWu2eu5hqXhSi7foKp1DGdraP7fa82kz1/h+/O5RDaYDZd4gEUz04I4bbxaPrSX6pVvzBYC5QqMuFk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-6c398717726so220876b3a.2 for ; Wed, 22 Nov 2023 12:43:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1700685814; x=1701290614; 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=pUWcj3BzUmRCryjJQvH1wKXPtoswVG7ctsf2+rf7hG0=; b=VisRCQ2o3ZwTx/l9NrE778X378wybfdM/BCimV+C0lErKuhT24f0+tZET5d7taGjFI 2zu3gAOidrhHLu7kfNf1St1Gk6cmsEVSXmzel0DibXlR1E7zX9YYUOyftCklUc0ZjCdg pmf50SFIAyGq+wCKEP0xya5+z5V5JkxXWXYFj9YMvMYxZnOj2HlBNLq0cz4verQ8IPHN opEiK/3lPiy0ZfyLJSNUP6usCuUm2lHwgFEGV44009Nwzi30HPVn7+VN9Dg+ufHbvckU k7DCv2CpyvKW1sP2BTrI4z3Uc4i23fGcgeA6LyNI7tjjuET1B+A5TcSb/nI7MlXm3avh en6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700685814; x=1701290614; 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=pUWcj3BzUmRCryjJQvH1wKXPtoswVG7ctsf2+rf7hG0=; b=Hnw7R73JI4344sFSlVaXD07VvBA6FN1ASqtpJ2pyjVXCgTlfaezHTfw1h2c5dOvhs0 vc7M+fTaeq1co3hD1HUA5FoXNt5s0LuGFKHnqgPvBoiZWED/oqhn/j9QoIu96eyjjwK8 0I5KFaFYqwxlcqW/ULjwYrlGjmMeBar8rEz35RfiNOFCARAEAAhrMwYtn8113RCL4hP9 0WXbI33c4srczrBoaIiK2j4I5TT2lut8LV8UzhZJGaAmyH+VZfr3SREY2XtUk/cULDSb ZozoWbju9e+N6VbxpCYKCboCxGZNhLDT4UejHPT7di1a3tigN+QV4NfWjgr6NsPzqTzx RgoQ== X-Gm-Message-State: AOJu0Yy0QJWSa3h3bC9+v/uHuidKIofSielR0h9L9ku2UQ7y0FSBL2oG kTX5wtxTjBROZA2kGPtwPYiQlg3YvnoJU3LaauW3JQ== X-Received: by 2002:a05:6a20:2589:b0:187:703a:8658 with SMTP id k9-20020a056a20258900b00187703a8658mr3988203pzd.59.1700685813926; Wed, 22 Nov 2023 12:43:33 -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.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Nov 2023 12:43:32 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, siddhesh@sourceware.org Subject: [PATCH v5 2/5] elf: Do not set invalid tunables values Date: Wed, 22 Nov 2023 17:43:22 -0300 Message-Id: <20231122204325.4058222-3-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=-12.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 The loader now warns for invalid and out-of-range tunable values. The patch also fixes the parsing of size_t maximum values, where _dl_strtoul was failing for large values close to SIZE_MAX. Checked on x86_64-linux-gnu. --- elf/dl-misc.c | 5 ++++- elf/dl-tunables.c | 35 ++++++++++++++++++++++++++++++----- elf/tst-tunables.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 6 deletions(-) diff --git a/elf/dl-misc.c b/elf/dl-misc.c index 5b84adc2f4..037cbb3650 100644 --- a/elf/dl-misc.c +++ b/elf/dl-misc.c @@ -190,6 +190,9 @@ _dl_strtoul (const char *nptr, char **endptr) } } + const uint64_t cutoff = (UINT64_MAX * 2UL + 1UL) / 10; + const uint64_t cutlim = (UINT64_MAX * 2UL + 1UL) % 10; + while (1) { int digval; @@ -207,7 +210,7 @@ _dl_strtoul (const char *nptr, char **endptr) else break; - if (result >= (UINT64_MAX - digval) / base) + if (result > cutoff || (result == cutoff && digval > cutlim)) { if (endptr != NULL) *endptr = (char *) nptr; diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 26161c68e7..67a37ff704 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -77,16 +77,27 @@ do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp, { tunable_num_t val, min, max; - if (cur->type.type_code == TUNABLE_TYPE_STRING) + switch (cur->type.type_code) { + case TUNABLE_TYPE_STRING: cur->val.strval = valp->strval; cur->initialized = true; return; + case TUNABLE_TYPE_INT_32: + val = (int32_t) valp->numval; + break; + case TUNABLE_TYPE_UINT_64: + val = (int64_t) valp->numval; + break; + case TUNABLE_TYPE_SIZE_T: + val = (size_t) valp->numval; + break; + default: + __builtin_unreachable (); } bool unsigned_cmp = unsigned_tunable_type (cur->type.type_code); - val = valp->numval; min = minp != NULL ? *minp : cur->type.min; max = maxp != NULL ? *maxp : cur->type.max; @@ -117,16 +128,24 @@ do_tunable_update_val (tunable_t *cur, const tunable_val_t *valp, /* Validate range of the input value and initialize the tunable CUR if it looks good. */ -static void +static bool tunable_initialize (tunable_t *cur, const char *strval, size_t len) { tunable_val_t val = { 0 }; if (cur->type.type_code != TUNABLE_TYPE_STRING) - val.numval = (tunable_num_t) _dl_strtoul (strval, NULL); + { + char *endptr = NULL; + uint64_t numval = _dl_strtoul (strval, &endptr); + if (endptr != strval + len) + return false; + val.numval = (tunable_num_t) numval; + } else val.strval = (struct tunable_str_t) { strval, len }; do_tunable_update_val (cur, &val, NULL, NULL); + + return true; } void @@ -226,7 +245,13 @@ parse_tunables (const char *valstring) } for (int i = 0; i < ntunables; i++) - tunable_initialize (tunables[i].t, tunables[i].value, tunables[i].len); + if (!tunable_initialize (tunables[i].t, tunables[i].value, + tunables[i].len)) + _dl_error_printf ("WARNING: ld.so: invalid GLIBC_TUNABLES value `%.*s' " + "for option `%s': ignored.\n", + (int) tunables[i].len, + tunables[i].value, + tunables[i].t->name); } /* Initialize the tunables list from the environment. For now we only use the diff --git a/elf/tst-tunables.c b/elf/tst-tunables.c index 188345b070..d6a1e1b3ac 100644 --- a/elf/tst-tunables.c +++ b/elf/tst-tunables.c @@ -53,6 +53,13 @@ static const struct test_t 4096, 0, }, + { + "GLIBC_TUNABLES", + "glibc.malloc.mmap_threshold=-1", + 0, + SIZE_MAX, + 0, + }, /* Empty tunable are ignored. */ { "GLIBC_TUNABLES", @@ -224,6 +231,29 @@ static const struct test_t 0, 0, }, + /* Invalid numbers are ignored. */ + { + "GLIBC_TUNABLES", + "glibc.malloc.check=abc:glibc.malloc.mmap_threshold=4096", + 0, + 4096, + 0, + }, + { + "GLIBC_TUNABLES", + "glibc.malloc.check=2:glibc.malloc.mmap_threshold=abc", + 2, + 0, + 0, + }, + { + "GLIBC_TUNABLES", + /* SIZE_MAX + 1 */ + "glibc.malloc.mmap_threshold=18446744073709551616", + 0, + 0, + 0, + }, /* Also check some tunable aliases. */ { "MALLOC_CHECK_",