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 X-Patchwork-Id: 81516 X-Patchwork-Delegate: siddhesh@gotplt.org 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 6E8E0385C40A for ; Wed, 6 Dec 2023 13:24:35 +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-Google-Smtp-Source: AGHT+IE+tRGb8sqsuRgvoGVqPne/nAIN5NuTnmi0p339SC+KdzolKomLn5ZF2Yix70Z2yOkom7ZpPA== 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+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. 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_",