From patchwork Fri Apr 19 03:49:30 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: DJ Delorie X-Patchwork-Id: 1925354 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=A+gIiHQS; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VLLJ405PSz1yPJ for ; Fri, 19 Apr 2024 13:49:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 20CA0384AB7C for ; Fri, 19 Apr 2024 03:49:53 +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.133.124]) by sourceware.org (Postfix) with ESMTPS id BF69A384AB5F for ; Fri, 19 Apr 2024 03:49:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BF69A384AB5F 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 BF69A384AB5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713498575; cv=none; b=KLWN0YHWiGh/1UhpJxtzJcqxry9y5cs2SPJYOHkniMicEA74xP8dzcatCveT3uswa56XmGJNFYWn+ZAIdtlEmzTQEELU6PBwdiJnQstM8iPbjb+3lBt3J1nPkOsdc+wtywNizx1QC6ORbIdV8l5gT+yc7r27dlgjB0ZX3x+MHW0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1713498575; c=relaxed/simple; bh=WaSHvVikh2E9x1/AqANS8KzWvuWOZRWkhLb8lrjW2mA=; h=DKIM-Signature:Date:Message-Id:From:To:Subject; b=SmG79iZxDlbiCTwaGBeWVQ8acZOPLoK0EBMNBLBBenZWHWsHLj9ocSJI9XHBL+jdoX3AJFJJM13JyEGwMMZ6Wu0HQYVnj6WVykdt+XsP6HWaiim5IAOzRJnqla1NRbEJ+fdp1prBdw+Ymy1vgcEKqWhQ6YgZawKKKVafz6brGGY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1713498573; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:content-type:content-type; bh=39ZYvgqv3Vgww0fX44q1QYg7Rs+Dl5/VMoBRDXVaiXI=; b=A+gIiHQSJBrwDgyNCatCUuzCZ2zmcRNZ7MbcFfMRuwp08VYMfLstze5ebv9O2g7C54XDEn 9cxyTdiFayIxibsf75QK2QQuiDY0qupMq51DrbDDqMZsF/ZVINyTZklf2L5BZ4VpgSd2tw 4I9H+LKjnM9H44FjPZQ99BOL7qhe2sc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-678-_CCAV1-kNO-fjet5YagoHA-1; Thu, 18 Apr 2024 23:49:31 -0400 X-MC-Unique: _CCAV1-kNO-fjet5YagoHA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 4A0AD80021A for ; Fri, 19 Apr 2024 03:49:31 +0000 (UTC) Received: from greed.delorie.com (unknown [10.22.8.231]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 364941C060D1 for ; Fri, 19 Apr 2024 03:49:31 +0000 (UTC) Received: from greed.delorie.com.redhat.com (localhost [127.0.0.1]) by greed.delorie.com (8.16.1/8.16.1) with ESMTP id 43J3nU2v1041570 for ; Thu, 18 Apr 2024 23:49:30 -0400 Date: Thu, 18 Apr 2024 23:49:30 -0400 Message-Id: From: DJ Delorie To: libc-alpha@sourceware.org Subject: [PATCH 3/3] Add system-wide tunables: Apply tunables part X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.5 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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+incoming=patchwork.ozlabs.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 d3ccd2ecd4..5ecddccff6 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) @@ -264,6 +265,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. */ @@ -273,6 +299,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);