From patchwork Tue Oct 17 13:05:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 734384 Delivered-To: patch@linaro.org Received: by 2002:adf:f0cd:0:b0:32d:baff:b0ca with SMTP id x13csp468817wro; Tue, 17 Oct 2023 06:07:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGgpMpkqufqXtD+8LK0gCFIQh9EzI6guV6Z/X7N6axOGZ7wYlzIlj97kZI374No9yoJZNMs X-Received: by 2002:a05:6122:989:b0:4a8:4218:804b with SMTP id g9-20020a056122098900b004a84218804bmr2640689vkd.12.1697548027122; Tue, 17 Oct 2023 06:07:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697548027; cv=pass; d=google.com; s=arc-20160816; b=R7UZW3DR+0RZrXjYzb8GB/c+88/BDq4yMSGept1C/Yq5StziptsYtBvHj2/wUAuDq6 Z1aB1iJHrdKhyfQgPtqctoH2GWuDuJCLgn5nWQH8d5NUfWhlYyVIxxfOa6gzH5ekWQbt omfO9t21tdIOyAkO+yldkkJV3HDeTd9aVrrYXOzmXgP6dB2FJ7pnKt5Zpmwm3uTOcmJQ PRI19mmbDbcLhDITsD62sSMqT6lOY4OiU86moasCZD291CHQKOLAqPangRGiu1sQXkrh NI05yfOmd8fg94bRWnd0zawU/igFZrhFCE3ccLL0FdbOgTEaV2utvFdE98bRhhGblWgD 4vsA== 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=vaad8SkS2CP0FoKSLVb85qDql5BCh9g7UP+E0r7ANbg=; fh=+FUb54tScwW7D3lvWhZcQBi30wyNNn2DusdH7ahfqKk=; b=XaamqGULtKMq88FlcAHka6dmGse8wKBeCXtnYpYqYfQ1HqwtQ0xE07jTV3jQdhCuDU zSJbg7wB6dvmumK2CH591X8iqHHhJ5RiPe108ll/+uJrlX+jxyGTeA9ror0qD7S8jsi7 UV4nsLTP4co3aHdVWPXoV1inUNwGU0+/iOGZqqSXB4rjs20E/bgkcOdz86Z0HX4F7GPS J4bLcmSA3mPwJ49AagN4LWjet7R5oGiEF0FaYXU01bNkQsEIZFwnixIul8O1Yr5GRvs+ TBxCwVUSXjC/sHWBPdRdfxXbIqY/JTkHAh1tKLR5f7PQCEkhWLdSzCLu7sxtioFv1wiB eCWg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dXdSwdOE; 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 r12-20020a1f2b0c000000b0048d13cdebd1si130370vkr.25.2023.10.17.06.07.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:07:07 -0700 (PDT) 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=dXdSwdOE; 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 6529B3860C2C for ; Tue, 17 Oct 2023 13:07:03 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x436.google.com (mail-pf1-x436.google.com [IPv6:2607:f8b0:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id 58C7E3858298 for ; Tue, 17 Oct 2023 13:06:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 58C7E3858298 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 58C7E3858298 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::436 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697547989; cv=none; b=q9owazG3CNaksSS/iEwzDnhgTEC32Z0JclXtP3q+o+7NpgxAp1s9598A7S11EH4CgnM7i/FYGAzzqHJLRPVMboTDtLPFmC2AiHHMT31n8lB4pFngTm3L/NtCqGM7WoZytRXUI+M9hQk4ytaBTkDGSD6UPnV3LID2HdAPr8KYIPw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697547989; c=relaxed/simple; bh=TvNgbJIV5G51STMzzsMDWypbPkMAPD+L4Oydae4DUHA=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=MARzQysOlJbNpAdAh3gkO5/n7gD3wF/6z3JMKbJtRh6tCa0BFsQZfeGv7RT0uCArDVYHsYOi20mcsnig9Ak2NX3vm7zfJggGJbJ2065ApqPaUi005hnq9HUEOEn2q6mQHCDpMcUzDi7iTtMVmFkCNQVPNm7ZLIBozVIyE9DV4Wg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x436.google.com with SMTP id d2e1a72fcca58-692c02adeefso4079090b3a.3 for ; Tue, 17 Oct 2023 06:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1697547987; x=1698152787; 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=vaad8SkS2CP0FoKSLVb85qDql5BCh9g7UP+E0r7ANbg=; b=dXdSwdOE6IRcxHZHN61smCLPgctlj+J9pOm9OUFthPPONSHwkiwba6g+HkTl6kJZIj /5+gl75G0mL83cpGX092k/ceLstMjNuG12I1zRYMdXeYAgY2F7p6WWhTEFY0IpANvB9Y gPz2l6aCYsnGbwev/BVjv470CIaBjhMASiEPTDXF0ZCFALy5UIYJEq/qlAm0+ZZOsXsg Y2e5FhDCzoJoLlYbHOOKcr48MMwabIKa+wRXgiKRkGl1pQxpeLFzLHNBldCCjEy0oT18 MuBilpNhNTUjLgSnU56w91ev+KuWYOr+ArdxzuM/tWcSbHrXNcez4zkm/aQP6EIEB8MR BR0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697547987; x=1698152787; 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=vaad8SkS2CP0FoKSLVb85qDql5BCh9g7UP+E0r7ANbg=; b=hx134ucimGD087hadQCiYZVDOthNgiSjm/FRkIHMtRiRRWfk8i80WIVvmHiuQv/e6Z qJKS2LOabtr8XbzZCSvGt9qlvIpr3T9qB4mP5Z0Sd1bDmkufJ4CKG6PVQJS++5VKwilg 17vhB12Bzx8m2VQZtfm72kwRyy7SbnZYLgoZHDfEnRABAy7kCe9CFK0W4QtlnZXvSarl Ej5FlUbNR8pSGhWuHmUdCv3yRi7vak+n2wtZyVv3IQBhdnJCxST+Cp01jJZI+dFlxDTN rnR9o6rqtJBqY4088QrWkRo9Sh4b1xb6wUBThu29SckhZ88F/TKiD4CGEboWM9JuBj9D o+OQ== X-Gm-Message-State: AOJu0Yy+pEMzkNQv07RFLu6JCSXWl+Mj2wSqL5DUp7Jh9kB4Qzubm5Bh BqUo8vHV5QMUNz6Mn8tmVTNt/lmaJWWvlNrYH3KAjQ== X-Received: by 2002:a05:6a21:778d:b0:157:609f:6057 with SMTP id bd13-20020a056a21778d00b00157609f6057mr1747573pzc.27.1697547986927; Tue, 17 Oct 2023 06:06:26 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:7f2e:11d:92b4:4d78:4197]) by smtp.gmail.com with ESMTPSA id l28-20020a635b5c000000b0056b6d1ac949sm1309788pgm.13.2023.10.17.06.06.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:06:26 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org, Siddhesh Poyarekar Subject: [PATCH v2 06/19] elf: Do not parse ill-formatted strings Date: Tue, 17 Oct 2023 10:05:13 -0300 Message-Id: <20231017130526.2216827-7-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231017130526.2216827-1-adhemerval.zanella@linaro.org> References: <20231017130526.2216827-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.7 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 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 Instead of ignoring ill-formatted tunable strings, first, check all the tunable definitions are correct and then set each tunable value. It means that partially invalid strings, like "key1=value1:key2=key2=value' or 'key1=value':key2=value2=value2' do not enable 'key1=value1'. It avoids possible user-defined errors in tunable definitions. Checked on x86_64-linux-gnu. --- elf/dl-tunables.c | 50 +++++++++++++++++++++++++++++++++++----------- elf/tst-tunables.c | 13 ++++++++---- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 59bee61124..5d4b8c5bc0 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -154,17 +154,29 @@ __tunable_set_val (tunable_id_t id, tunable_val_t *valp, tunable_num_t *minp, do_tunable_update_val (cur, valp, minp, maxp); } -/* Parse the tunable string VALSTRING. VALSTRING is a duplicated values, - where delimiters ':' are replaced with '\0', so string tunables are null - terminated. */ -static void -parse_tunables (char *valstring) +struct tunable_toset_t +{ + tunable_t *t; + const char *value; +}; + +enum { tunables_list_size = array_length (tunable_list) }; + +/* Parse the tunable string VALSTRING and set TUNABLES with the found tunables + and their respectibles values. VALSTRING is a duplicated values, where + delimiters ':' are replaced with '\0', so string tunables are null + terminated. + Return the number of tunables found (including 0 if the string is empty) + or -1 if for a ill-formatted definition. */ +static int +parse_tunables_string (char *valstring, struct tunable_toset_t *tunables) { if (valstring == NULL || *valstring == '\0') - return; + return 0; char *p = valstring; bool done = false; + int ntunables = 0; while (!done) { @@ -177,7 +189,7 @@ parse_tunables (char *valstring) /* If we reach the end of the string before getting a valid name-value pair, bail out. */ if (*p == '\0') - break; + return -1; /* We did not find a valid name-value pair before encountering the colon. */ @@ -190,30 +202,44 @@ parse_tunables (char *valstring) /* Skip the ':' or '='. */ p++; - const char *value = p; + char *value = p; while (*p != '=' && *p != ':' && *p != '\0') p++; if (*p == '=') - break; + return -1; else if (*p == '\0') done = true; else *p++ = '\0'; /* Add the tunable if it exists. */ - for (size_t i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++) + for (size_t i = 0; i < tunables_list_size; i++) { tunable_t *cur = &tunable_list[i]; if (tunable_is_name (cur->name, name)) { - tunable_initialize (cur, value); + tunables[ntunables++] = (struct tunable_toset_t) { cur, value }; break; } } } + + return ntunables; +} + +static void +parse_tunables (char *valstring) +{ + struct tunable_toset_t tunables[tunables_list_size]; + int ntunables = parse_tunables_string (valstring, tunables); + if (ntunables == -1) + return; + + for (int i = 0; i < ntunables; i++) + tunable_initialize (tunables[i].t, tunables[i].value); } /* Initialize the tunables list from the environment. For now we only use the @@ -240,7 +266,7 @@ __tunables_init (char **envp) continue; } - for (int i = 0; i < sizeof (tunable_list) / sizeof (tunable_t); i++) + for (int i = 0; i < tunables_list_size; i++) { tunable_t *cur = &tunable_list[i]; diff --git a/elf/tst-tunables.c b/elf/tst-tunables.c index 03039b5260..e124fa4c6d 100644 --- a/elf/tst-tunables.c +++ b/elf/tst-tunables.c @@ -161,7 +161,7 @@ static const struct test_t 0, 0, }, - /* If there is a ill-formatted key=value, everything after is also ignored. */ + /* Ill-formatted tunables string is not parsed. */ { "glibc.malloc.mmap_threshold=glibc.malloc.mmap_threshold=4096:glibc.malloc.check=2", 0, @@ -186,13 +186,18 @@ static const struct test_t 0, 0, }, - /* Valid tunables set before ill-formatted ones are set. */ { "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096", - 2, 0, 0, - } + 0, + }, + { + "glibc.malloc.check=2:glibc.malloc.mmap_threshold=4096=4096", + 0, + 0, + 0, + }, }; static int