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: 78019 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 1956D386192A for ; Tue, 17 Oct 2023 13:07:13 +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-Google-Smtp-Source: AGHT+IGuxjMmnM9/bBmJBVYvCGtYJpScNwLjrkpH+uiPXdmJ1EADuUkrLmpTEvdNgdiW6I4XKF3ZeA== 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+patchwork=sourceware.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