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: 80583 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A0196393BA5D for ; Wed, 22 Nov 2023 20:44:04 +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-Google-Smtp-Source: AGHT+IHXW+/zMHNOgF4v6Ww/wrJkCxR2bSPsHcMBtm1eX/KJ0Ua3mUfM7sWlv7U2NafK7OoilpAbtA== 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+patchwork=sourceware.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_",