From patchwork Wed Dec 6 13:24:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 750727 Delivered-To: patch@linaro.org Received: by 2002:adf:9b9d:0:b0:333:3a04:f257 with SMTP id d29csp2241149wrc; Wed, 6 Dec 2023 05:24:27 -0800 (PST) X-Google-Smtp-Source: AGHT+IFi9CR7PazzLTnEBBNnZ/wPX6lfm7ydTvf+VBKpCdHnCzItD3AEfKNC+1b4oiqC1EbCrMP7 X-Received: by 2002:a05:620a:2609:b0:77f:dca:c213 with SMTP id z9-20020a05620a260900b0077f0dcac213mr1015120qko.37.1701869067486; Wed, 06 Dec 2023 05:24:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1701869067; cv=pass; d=google.com; s=arc-20160816; b=lFa646pPw2ETa/2153dOwptDi7CMzarmqWusiU8mpI0Hyk26fp8pjFZ4VGjNYInFv0 +GNdI1Frx+RLKNagUQC5tHfQfnV2BG4xgzJWWT+VrfQcs97B8tEhMvGoT6qSJwkdHlCy pLgmSIgqnJ8gaGvfsWVCi2B6e1nv0E3SF3kB6/SBQyvW3DRlQWq6+rc8YX7SlrA0hUou ZpSdYgFUvOcTyj6NWNqOElXX4NbessVyQeKTLSSlFE8r8xa7YFkJH6Nda+6mQUd5Dj0K X3O5vM+PI+yOfOD8TqszPp6+0sP54KhKVwvSaSPB5T2QxxkO6B+dZuz6HwGeRadhmM9M Nw6A== 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=rR8lFoAqfh815xPRUlvhxBMZdVIa6yUVq0+rvrr6R9s=; fh=+FUb54tScwW7D3lvWhZcQBi30wyNNn2DusdH7ahfqKk=; b=CEAXup+quVB+hx/B+eTdFd/iH/8wGAgObAA4gBzriBlVErtrlalclq1NuTeXmoBXnc j4DiNqAvoWLTkGo4xUI87d0iLqCI9EzOrUNTY/6PwO6H/WCXfU8oIpOA7zktl6dYGja5 0fu4i6zBnYFk1GcCaGnjqlBtvId5u0UlMDwOReX08DlSe9PhF1gT6T82nfNRbipBM24k AyFm/PUeP7d9VoiCMzDBaiXJNzT3UJ9yoAvXqkuZ3gkAweR53NmQs57pJgi6+IRGLsOG NkYF/aGZxmUv1FqlNib7/ec0Sf+mmOrqgpoToy7YflhUJWt5UcaBDKoT9xWr9YQolKIL 5qGg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=HKA1PA7G; 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 s16-20020a05620a255000b007742f841ec5si14596946qko.383.2023.12.06.05.24.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 05:24:27 -0800 (PST) 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=HKA1PA7G; 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 2A0853856DCD for ; Wed, 6 Dec 2023 13:24:27 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x534.google.com (mail-pg1-x534.google.com [IPv6:2607:f8b0:4864:20::534]) by sourceware.org (Postfix) with ESMTPS id 9E82C3858429 for ; Wed, 6 Dec 2023 13:24:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9E82C3858429 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 9E82C3858429 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::534 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701869057; cv=none; b=f6Gg+boXMKEDoO+ohNc4lkjku+oMJ7JVJd30TDe5jOCoIuSfmsAvQO26ob9+eMOMa/BYBJth61NLy9Ou4z75vPg+hWpvFl+nuZ9kqmTHN3eTwgcL+iZe+thDIW/EhBoS/S64/GxMDxx4V0/14kKwwt8MmGBOQINuTtldrn+7R7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701869057; c=relaxed/simple; bh=sLyYJf7sUmoeSsNakhy3RLvZPqTDoph3MoEwqrgkr8s=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fQFqahSzexePbdzoYej30iTHlVm+H4dQgfTlcE94xWI7hcOgOeNH1Ze+yZgVavY1v2wp7kvgqKGXGqttZMbZuSqgBGCgDhptJt7i3ACRz6Vkxu+l87pT1yQgwEZBeRmUbe0rb3OpQTvGR1xlhxOai0/9Uagc6H65rjMhQWmzEu4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x534.google.com with SMTP id 41be03b00d2f7-5c5f0e325a6so2728697a12.1 for ; Wed, 06 Dec 2023 05:24:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1701869051; x=1702473851; 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=rR8lFoAqfh815xPRUlvhxBMZdVIa6yUVq0+rvrr6R9s=; b=HKA1PA7GRZfElAsWmVpjOof8WgLuq3uis5nqwHPBdvwnWk/R0zaS9z4mqa6Ooxu5J8 pZPAGm07D2CsQveJw2JpsBCqsuXQHYoUCYyLxI5FuNdvAsj9Bm1+/4iPQii6y5vjcbyq SwykCRkumnQay2l/f46pnWCAeZnU2L6HH/mKy7ta1WJ76U7qnQZtGnpnKLJnkBQ+be94 CI0Y1ex9r/e4w5UPKanvxBKCnSC23krGi+QmbOD0bvP0oFmn9Qh2uIM7/hhj0SvBzdx1 bGQc/cnSYv/KP+7/ocO/UeSAq5j5JPhBlB36//k46DoTDgOnYkUjHGcoJcanw05MmmVe EJtQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701869051; x=1702473851; 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=rR8lFoAqfh815xPRUlvhxBMZdVIa6yUVq0+rvrr6R9s=; b=lCyJrlOFwJc+4qb2t8Ml62X//8vcz4LQ+nUSMFktiwnmcFenl3EvERIahYvunh7ksp 9oB69lo2aFMDNksev3tcuu8bunI3V150pPJPMxnlys5tGb3mCYydximAYp3LdAChRmPW 9LhFO9eajxM7PkMSkuMZinnTlQBX3Uzo/X0/sdgZtxDoKrlN83RrEAFpCFu2GaPstSXt 24oejYp2n/kXiHyZ55oQ4i8xt5wziMtVfMV2nxV/R+Mk52CfaRuOMdFHT/cxMC5JTyhq Lpqp7kfERFskdo0z3HamxXh1KhiAi45hcqERX1aomCevj1GYMkvhnJtPHKwUUejc2iZU VyEQ== X-Gm-Message-State: AOJu0Yyhb0wbi4wxCXdGsNxXCZfK5GpUEfTnSG6ZXiIS0kUODRocS4HC tuTCCawFfRrpTfdwTldLB77e7h16iqQkfkB/dJQ= X-Received: by 2002:a05:6a20:7b06:b0:18f:b870:e9b3 with SMTP id s6-20020a056a207b0600b0018fb870e9b3mr570614pzh.121.1701869050810; Wed, 06 Dec 2023 05:24:10 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c3:56e1:da8e:9a8e:e071:49b6]) by smtp.gmail.com with ESMTPSA id b19-20020a17090ae39300b0028672a85808sm7449168pjz.35.2023.12.06.05.24.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Dec 2023 05:24:09 -0800 (PST) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH v6 2/2] elf: Do not set invalid tunables values Date: Wed, 6 Dec 2023 10:24:02 -0300 Message-Id: <20231206132402.1356644-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231206132402.1356644-1-adhemerval.zanella@linaro.org> References: <20231206132402.1356644-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, 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. Reviewed-by: Siddhesh Poyarekar --- elf/dl-misc.c | 10 +++++++++- elf/dl-tunables.c | 35 ++++++++++++++++++++++++++++++----- elf/tst-tunables.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) diff --git a/elf/dl-misc.c b/elf/dl-misc.c index 5b84adc2f4..8dccb3a9a9 100644 --- a/elf/dl-misc.c +++ b/elf/dl-misc.c @@ -174,6 +174,9 @@ _dl_strtoul (const char *nptr, char **endptr) return 0UL; } + uint64_t cutoff = (UINT64_MAX * 2UL + 1UL) / 10; + uint64_t cutlim = (UINT64_MAX * 2UL + 1UL) % 10; + int base = 10; max_digit = 9; if (*nptr == '0') @@ -182,14 +185,19 @@ _dl_strtoul (const char *nptr, char **endptr) { base = 16; nptr += 2; + cutoff = (UINT64_MAX * 2UL + 1UL) / 16; + cutlim = (UINT64_MAX * 2UL + 1UL) % 16; } else { base = 8; max_digit = 7; + cutoff = (UINT64_MAX * 2UL + 1UL) / 8; + cutlim = (UINT64_MAX * 2UL + 1UL) % 8; } } + while (1) { int digval; @@ -207,7 +215,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 3d41e8e28e..092b696484 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -69,16 +69,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; @@ -109,16 +120,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; } bool @@ -225,7 +244,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_",