From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from bumble.maple.relay.mailchannels.net (bumble.maple.relay.mailchannels.net [23.83.214.25]) by sourceware.org (Postfix) with ESMTPS id 8BC0C3851C04 for ; Wed, 14 Apr 2021 05:04:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8BC0C3851C04 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 4E9D49215A4 for ; Wed, 14 Apr 2021 05:04:36 +0000 (UTC) Received: from pdx1-sub0-mail-a20.g.dreamhost.com (100-96-16-47.trex.outbound.svc.cluster.local [100.96.16.47]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id EF3BD921975 for ; Wed, 14 Apr 2021 05:04:33 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a20.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.96.16.47 (trex/6.1.1); Wed, 14 Apr 2021 05:04:36 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Snatch-Vacuous: 0d74993a596a0990_1618376676041_1204613005 X-MC-Loop-Signature: 1618376676041:1445899688 X-MC-Ingress-Time: 1618376676041 Received: from pdx1-sub0-mail-a20.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a20.g.dreamhost.com (Postfix) with ESMTP id B24337EFBB for ; Tue, 13 Apr 2021 22:04:33 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a20.g.dreamhost.com (Postfix) with ESMTPSA id A275E7EFB7 for ; Tue, 13 Apr 2021 22:04:32 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a20 From: Siddhesh Poyarekar To: libc-stable@sourceware.org Subject: [COMMITTED 2.33 1/7] tunables: Fix comparison of tunable values Date: Wed, 14 Apr 2021 10:34:16 +0530 Message-Id: <20210414050422.981607-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3495.0 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Apr 2021 05:04:40 -0000 The simplification of tunable_set interfaces took care of signed/unsigned conversions while setting values, but comparison with bounds ended up being incorrect; comparing TUNABLE_SIZE_T values for example will fail because SIZE_MAX is seen as -1. Add comparison helpers that take tunable types into account and use them to do comparison instead. (cherry picked from commit d1a3dcabf2f89233a99a4a9be08f9f407da0b6b4) --- elf/dl-tunable-types.h | 17 +++++++++++++++++ elf/dl-tunables.c | 25 ++++++++++++++----------- elf/dl-tunables.h | 18 ++++++++++++++++++ 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/elf/dl-tunable-types.h b/elf/dl-tunable-types.h index 626ca334be..39bf738d93 100644 --- a/elf/dl-tunable-types.h +++ b/elf/dl-tunable-types.h @@ -81,4 +81,21 @@ struct _tunable =20 typedef struct _tunable tunable_t; =20 +static __always_inline bool +unsigned_tunable_type (tunable_type_code_t t) +{ + switch (t) + { + case TUNABLE_TYPE_INT_32: + return false; + case TUNABLE_TYPE_UINT_64: + case TUNABLE_TYPE_SIZE_T: + return true; + case TUNABLE_TYPE_STRING: + default: + break; + } + __builtin_unreachable (); +} + #endif diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index a2be9cde2f..8b751dcf0d 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -107,32 +107,35 @@ do_tunable_update_val (tunable_t *cur, const tunabl= e_val_t *valp, return; } =20 + bool unsigned_cmp =3D unsigned_tunable_type (cur->type.type_code); + val =3D valp->numval; min =3D minp !=3D NULL ? *minp : cur->type.min; max =3D maxp !=3D NULL ? *maxp : cur->type.max; =20 /* We allow only increasingly restrictive bounds. */ - if (min < cur->type.min) + if (tunable_val_lt (min, cur->type.min, unsigned_cmp)) min =3D cur->type.min; =20 - if (max > cur->type.max) + if (tunable_val_gt (max, cur->type.max, unsigned_cmp)) max =3D cur->type.max; =20 /* Skip both bounds if they're inconsistent. */ - if (min > max) + if (tunable_val_gt (min, max, unsigned_cmp)) { min =3D cur->type.min; max =3D cur->type.max; } =20 - /* Write everything out if the value and the bounds are valid. */ - if (min <=3D val && val <=3D max) - { - cur->val.numval =3D val; - cur->type.min =3D min; - cur->type.max =3D max; - cur->initialized =3D true; - } + /* Bail out if the bounds are not valid. */ + if (tunable_val_lt (val, min, unsigned_cmp) + || tunable_val_lt (max, val, unsigned_cmp)) + return; + + cur->val.numval =3D val; + cur->type.min =3D min; + cur->type.max =3D max; + cur->initialized =3D true; } =20 /* Validate range of the input value and initialize the tunable CUR if i= t looks diff --git a/elf/dl-tunables.h b/elf/dl-tunables.h index ba7ae6b52e..3880e4aab6 100644 --- a/elf/dl-tunables.h +++ b/elf/dl-tunables.h @@ -115,6 +115,24 @@ rtld_hidden_proto (__tunable_set_val) /* The default value for TUNABLES_FRONTEND. */ # define TUNABLES_FRONTEND_yes TUNABLES_FRONTEND_valstring =20 +static __always_inline bool +tunable_val_lt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp) +{ + if (unsigned_cmp) + return (uintmax_t) lhs < (uintmax_t) rhs; + else + return lhs < rhs; +} + +static __always_inline bool +tunable_val_gt (tunable_num_t lhs, tunable_num_t rhs, bool unsigned_cmp) +{ + if (unsigned_cmp) + return (uintmax_t) lhs > (uintmax_t) rhs; + else + return lhs > rhs; +} + /* Compare two name strings, bounded by the name hardcoded in glibc. */ static __always_inline bool tunable_is_name (const char *orig, const char *envname) --=20 2.29.2