From patchwork Wed Feb 28 04:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 86503 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 7B0243858C41 for ; Wed, 28 Feb 2024 04:44:55 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 0AD173858418 for ; Wed, 28 Feb 2024 04:44:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0AD173858418 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0AD173858418 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709095467; cv=none; b=oQxGmXb2bPMuRGiJeLcofzAZvByuiOIPLZXY5Ei2sJ+6SGXMqAx69q4kw64I6LsTOfACaWG0/Am9Db/WQdqzwhepq6ep8zX6c66553XWSaqFVv3fI1POQppr8Kvf7r1qpR9Ivi/sLhiSN1aByz8Z2Q9z4AEQfvrm/bh2GNlnQHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709095467; c=relaxed/simple; bh=NWL0+6QRw0dLIrHAc69pGrcrrAu6DUK2uWB9akgVIT0=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=hhZLeC/jD8Ka9zi3dRQKTc5aimcxxtIc5Jefv/Gwgy1jCbiSa32lLzGGAO7PPgAHBpRfFsTWg9UgMdloiKS/mahDGJ88yr9ng2UL6gzsFboFrFVG5L/Fl89PdGnh7Pu+nRUYG8uVG8ledX/ykOhjRjnwAVIvnWACX/PMWwgOrPM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709095464; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type; bh=Rqyn0UP1T+ZY/rztKEl5tzefc8r4/EKWp1kfWFxPijE=; b=B7PX/kMJrFIhrz679fU9r0oUsA7ZGmp9ENte5jwePkhQ3TtP9mi+QB1cdWf0IeJsUtz9DP ipJ9qYfvKPiW6yeCdbrYAm3HK0zRT/D2vlsTf0lMYJyBGsXGeu/c43pGErngqxMy05oWqz tNxPuiOEoFe5VtpZchn32LrvW8UIp3M= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-53-mvq_jlulPjGqNTObJgpyJg-1; Tue, 27 Feb 2024 23:44:22 -0500 X-MC-Unique: mvq_jlulPjGqNTObJgpyJg-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C279D3822E86 for ; Wed, 28 Feb 2024 04:44:19 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.122]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AD45D1130F for ; Wed, 28 Feb 2024 04:44:19 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.15.2/8.15.2) with ESMTP id 41S4iJ2b2239577 for ; Tue, 27 Feb 2024 23:44:19 -0500 Date: Tue, 27 Feb 2024 23:44:19 -0500 Message-Id: From: DJ Delorie To: libc-alpha@sourceware.org Subject: [PATCH v1 3/3] Add system-wide tunables: Apply tunables part X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham 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 Load ld.so.cache and fetch the tunables extension. Apply those tunables to the current program. We do not yet apply security policies. --- elf/dl-cache.c | 24 +++++++++++++ elf/dl-tunables.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++ elf/tunconf.h | 3 ++ 3 files changed, 112 insertions(+) diff --git a/elf/dl-cache.c b/elf/dl-cache.c index d90278889d..7ccb96d1ca 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -28,6 +28,7 @@ #include #include #include +#include "tunconf.h" #ifndef _DL_PLATFORMS_COUNT # define _DL_PLATFORMS_COUNT 0 @@ -616,3 +617,26 @@ _dl_unload_cache (void) now. */ } #endif + +const struct tunable_header_cached * +_dl_load_cache_tunables (const char **data) +{ + struct cache_extension_all_loaded ext; + + /* This loads the cache (temporary). */ + if (_dl_check_ldsocache_needs_loading ()) + _dl_maybe_load_ldsocache (); + + if (cache_new && cache_new != (void *) -1) + *data = (const char *) cache_new; + else + *data = (const char *) &cache->libs[cache->nlibs]; + + if (!cache_extension_load (cache_new, cache, cachesize, &ext)) + return NULL; + + /* Validate length/contents here. */ + if (ext.sections[cache_extension_tag_tunables].size < sizeof(struct tunable_header_cached)) + return NULL; + return ext.sections[cache_extension_tag_tunables].base; +} diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 03e1a68675..72d0804149 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -35,6 +35,7 @@ #define TUNABLES_INTERNAL 1 #include "dl-tunables.h" +#include "tunconf.h" static char ** get_next_env (char **envp, char **name, char **val, char ***prev_envp) @@ -253,6 +254,31 @@ parse_tunables (const char *valstring) tunables[i].t->name); } +/* We need this functionality before we load the real strcmp function. */ +static int +dl_strcmp(const char *s1, const char *s2) +{ + while (*s1 && *s2) + { + if (*s1 != *s2) + return *s1 - *s2; + ++s1; + ++s2; + } + return *s1 - *s2; +} +static int +dl_strlen(const char *s) +{ + int len = 0; + while (*s) + { + ++s; + ++len; + } + return len; +} + /* Initialize the tunables list from the environment. For now we only use the ENV_ALIAS to find values. Later we will also use the tunable names to find values. */ @@ -262,6 +288,65 @@ __tunables_init (char **envp) char *envname = NULL; char *envval = NULL; char **prev_envp = envp; + const struct tunable_header_cached *thc; + const char *td; + + thc = _dl_load_cache_tunables (&td); + if (thc != NULL) + { + for (int t = 0; t < thc->num_tunables; ++ t) + { + const struct tunable_entry_cached *tec = &( thc->tunables[t] ); + int tid = tec->tunable_id; + const char *name = td + tec->name_offset; + const char *value = td + tec->value_offset; + + /* Check that we have the correct tunable, and search by + name if needed. We rely on order of operations here to + avoid mis-indexing tunables[]. */ + if (tid < 0 || tid > tunables_list_size + || dl_strcmp (name, tunable_list[tid].name) != 0) + { + /* It does not, search by name instead. */ + tid = -1; + for (int i = 0; i < tunables_list_size; i++) + { + if (dl_strcmp (name, tunable_list[i].name) == 0) + { + tid = i; + break; + } + } + if (tid == -1) + continue; + } + /* At this point, TID is valid for the tunable we want. See + if the parsed type matches the desired type. */ + + if (tunable_list[tid].type.type_code == TUNABLE_TYPE_STRING) + { + /* This is a memory leak but there's no easy way around + it. */ + tunable_list[tid].val.strval.str = __strdup (value); + tunable_list[tid].val.strval.len = strlen (value); + } + else + { + tunable_val_t tval; + if (tec->flags & TUNCONF_FLAG_PARSED) + { + tval.numval = tec->parsed_value; + do_tunable_update_val (& tunable_list[tid], + &tval, NULL, NULL); + } + else + { + tunable_initialize (& tunable_list[tid], + value, dl_strlen(value)); + } + } + } + } /* Ignore tunables for AT_SECURE programs. */ if (__libc_enable_secure) diff --git a/elf/tunconf.h b/elf/tunconf.h index a6c5f0dd9a..623fb7546d 100644 --- a/elf/tunconf.h +++ b/elf/tunconf.h @@ -38,3 +38,6 @@ void parse_tunconf (const char *filename, int do_chroot, char *opt_chroot); struct tunable_header_cached * get_tunconf_ext (uint32_t str_offset); #define TUNCONF_SIZE(thc_p) (sizeof(struct tunable_header_cached) \ + thc_p->num_tunables * sizeof (struct tunable_entry_cached)) + +extern const struct tunable_header_cached * +_dl_load_cache_tunables (const char **data);