From patchwork Mon Jan 18 16:24:08 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Szabolcs Nagy X-Patchwork-Id: 41747 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 9D2B8383440B; Mon, 18 Jan 2021 16:24:45 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9D2B8383440B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1610987085; bh=KA199iensiCqNoHViABo7DqsWTK/yTw0d2vu4x32JOY=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=VLfD5MItojgxpXDsJpf4y/G8DQu7EQN+xljwPSdBebpWNeNESLEyAMT3bgn1PsdWA HiCNEHc4bbEV8TMvHRv6XnVZEi25AtCEmJVqER9Y6jQoNKPOxmXNjedzXmiQ6Bqoyc 8N3Va07/CSVvWO8BAsTy+AzBnk40XrEqgpcdjs5o= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from EUR02-VE1-obe.outbound.protection.outlook.com (mail-ve1eur02hn2231.outbound.protection.outlook.com [52.100.10.231]) by sourceware.org (Postfix) with ESMTPS id 1A4BA383440B for ; Mon, 18 Jan 2021 16:24:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1A4BA383440B Received: from AM6P193CA0142.EURP193.PROD.OUTLOOK.COM (2603:10a6:209:85::47) by AM6PR08MB3926.eurprd08.prod.outlook.com (2603:10a6:20b:a7::14) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.11; Mon, 18 Jan 2021 16:24:41 +0000 Received: from AM5EUR03FT026.eop-EUR03.prod.protection.outlook.com (2603:10a6:209:85:cafe::6a) by AM6P193CA0142.outlook.office365.com (2603:10a6:209:85::47) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10 via Frontend Transport; Mon, 18 Jan 2021 16:24:41 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; sourceware.org; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;sourceware.org; dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by AM5EUR03FT026.mail.protection.outlook.com (10.152.16.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.12 via Frontend Transport; Mon, 18 Jan 2021 16:24:40 +0000 Received: ("Tessian outbound e989e14f9207:v71"); Mon, 18 Jan 2021 16:24:40 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: 703767d0863b3712 X-CR-MTA-TID: 64aa7808 Received: from f9aa9d739290.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id D2F50564-BBC7-44E2-AD2B-B8B23392ECF8.1; Mon, 18 Jan 2021 16:24:16 +0000 Received: from EUR04-HE1-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id f9aa9d739290.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Mon, 18 Jan 2021 16:24:16 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=ftMnXPDoj5nO/mLeUMrsQggTcrhGNerv3bG6g68bpbWfpeCOQ/kSdz2mfG8C3IC5YQefwwhaLvmnht+/DJelEqTbuGtlImo1Hqp74qJ6eChLf7HFEOKKaZlNw+YJg7pVXOPl1Sb3c/mhoAsQfjn0mUvJHW+KKfLp26m2n2zfQiq43F0ooy0JvpRcYeZjyOryHmbT2yojvIHadcpeIVpQjy8NJnFs3aztNa1nXq3uW/Jy7qXwQlW79buBmX4kpawFUCJT4wwlnomkaEAVD80Lp7YxOTi3yv7ry1cU3qtWR54k4/qeeJsdqqpVdg2CyRFkOYw7nPOh/36eSbf5fWof3Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=KA199iensiCqNoHViABo7DqsWTK/yTw0d2vu4x32JOY=; b=CbSrswru/0NdkNNurF3WNdizfv/OguIolt6KIkQQYHaH/y7xcwFkSs4jkh6dXQ1xvBAnUaPm4X2gCu1T6YMmubJUrpUhX8drJIHkyOUXq+MK/5BQN61luVE7kfLTqKRGYE3baayoElinrb8/0DPsVTrs4YeCtNygQxM+lS8BMJ3Srl8MkxH+sRWdUxcIwKyL6C9oV3MbIllVVHeMy/NkERZWpw7zmP+t7MGwTCkwR4BqNllyd69lTN2WHxPbUHOynDvNUPo3eR+YEdtOA9mRvJYK8ThZXybuGg3TJtQQeL+xMUcCz7q0u5Lrb2CoWYLYRvfOAcgqIdq8PCUpnS57Zg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=arm.com; dmarc=pass action=none header.from=arm.com; dkim=pass header.d=arm.com; arc=none Authentication-Results-Original: sourceware.org; dkim=none (message not signed) header.d=none;sourceware.org; dmarc=none action=none header.from=arm.com; Received: from PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) by PA4PR08MB6238.eurprd08.prod.outlook.com (2603:10a6:102:e8::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.11; Mon, 18 Jan 2021 16:24:15 +0000 Received: from PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::700f:ddbe:a347:ee4f]) by PA4PR08MB6320.eurprd08.prod.outlook.com ([fe80::700f:ddbe:a347:ee4f%7]) with mapi id 15.20.3763.014; Mon, 18 Jan 2021 16:24:15 +0000 To: libc-alpha@sourceware.org Subject: [PATCH v4 04/10] elf: Avoid RELATIVE relocs in __tunables_init Date: Mon, 18 Jan 2021 16:24:08 +0000 Message-Id: <13b7b3186927350d9037a855b0b767c3be847562.1610986541.git.szabolcs.nagy@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: References: X-Originating-IP: [217.140.106.54] X-ClientProxiedBy: LO4P123CA0057.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:153::8) To PA4PR08MB6320.eurprd08.prod.outlook.com (2603:10a6:102:e5::9) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (217.140.106.54) by LO4P123CA0057.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:153::8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3763.10 via Frontend Transport; Mon, 18 Jan 2021 16:24:14 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 1b370800-50c8-425a-eab9-08d8bbcd8e96 X-MS-TrafficTypeDiagnostic: PA4PR08MB6238:|AM6PR08MB3926: X-Microsoft-Antispam-PRVS: x-checkrecipientrouted: true NoDisclaimer: true X-MS-Oob-TLC-OOBClassifiers: OLM:9508;OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: g7oaLSKpjLg5mq7zUm9ULBlbBN4hT9YhluYXNtuSLt6bTu+1gUB0KGuOo+pfx4hWWXGpHKNSr5wkwuhErgNwLgAkS1zanNwxnHvZDMnGz2e2tiCsEOO2B/LviT0/jFwt1xfF485awHmQPcgZVT8ZNqE45NY0GKs4XM83dIbxicGgl+hzBUKTlEw1zQT6A5zm7Tdl9rMwWwjKH54dtsG4ffB4G43pOjLzB+wnC67cDs2b9bejeF46FMSy2F//htS6HA3u7p34Hg4XQup65+SqJZRsdRfgoHGsnLyLksLAcKyveWqRpqtZ96+Pzw1c9Zz/EXYs8mcElxSTe6ke/JkxbkXIZIPTRF2N9f8aEDH49uM2wCxwm/Mwmcqn7OIqN85lhxALe8MYnBeLh02lANd3Z/brUH5ZOs8NCZpy6XOox5HV3CTuvHTSVtuxlU5uv3Wn1rnrwl3BPK8foO2FxegKklPmIlCYLEKyVvRR0OMxxAYatPrO0uLr138tKrNR/rIMlI42X1qnFfFdWkKLATSB+5/r7psnVHQToqpiZYpBGzdqp9A18Ypd1h/834EoePrX8sV6BUBTHFnsLNVt3oC4tA== X-Forefront-Antispam-Report-Untrusted: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:PA4PR08MB6320.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(366004)(39850400004)(376002)(396003)(136003)(346002)(83380400001)(6486002)(8676002)(69590400011)(478600001)(6666004)(52116002)(6916009)(316002)(8936002)(66476007)(66556008)(66946007)(5660300002)(86362001)(16526019)(26005)(6512007)(2906002)(186003)(6506007)(956004)(2616005)(36756003)(44832011); DIR:OUT; SFP:1101; X-MS-Exchange-AntiSpam-MessageData: bHshbvtPD5wxCpFsVzP3mErh8vHv4YfApXZ/wE1YQ3aYQqYJLfU0xVDnVg61iP9OZlb+ikIWAuleE2azC4a1xO/pSwXACieCKQ14m1LjsXiNBYI8gYmDmPdrCA7vXiX0Cjl+PZ02pyPYSoifBhpUHANjs2ctWzVfVv1M3W3y2WfxE81B7lol4x5DI0UYfk0euQVxsAsk0YA8pXObIWOfq1iXIooNtO4o8HPBVfwZp3DJ2bzcbZ+vDE+g9V9zTAGrNdJ7WqH9/BSz/6WFIkyxj43xM+stnX0EIW25W7VlSjsYZ3rJRiJFQVuXl7XGC6VhhAI7jM4Krk4EhB7mBey6qSQ6k/hkxBNElN6rN8gkzFvqeutUaN1TWL52CRF41gDnUUee7Qryh2a1Fj+g50rc+EUvs9eUhOc0+azKtBdQCV1nVhUF2wtX3oFtWQuCFZHd3eh1f8dI3tgejYPW+x1izzgvlbv+xo+fTCu4dmo9uhX1N2HacBx7szGA5bwewgk+oLkgdnRW4PFSu24w15Bqiw0RL1FfRdQO31hfeYWOE8UqVG8Ti66eQlbmrlvtXbkrIk0xsr82YZLIMKTYdqX0r5K4A5xhQb2232bW0QV3VJC1PMnZ2c75HugtnHeuzyXHz0gCI1DCzC5O1pBDx7R7VvYbZG17F+adq0Ad6jGoQDczhYvwDnlznnx4AK5rWQjrkLfEZyONBmbvZLhmp4bsbNtekc5uPg0IUmyxxIdUGjig4+3/Uyhfcii90Zb2qyV+0BSGohqQx2wyRPjNE3X6CGy8CMYY5vq2TLYBU3lzmDK11RBG/PcB7ftcWhLvqwclNDTf4E/7ZFDT2lQ7VwCTRu0X0gI7X/v5c/Drzb62ZhtUkTFmFOCNVtuMPzUpr6Dg2lB2ajT5pXlay3lGZOdHwQMD2Lesxnwvpg8TpLN4lD3kLWUkiXcEZx+tXWn1UOuj6XS7tXwmXmxFJgARwd4dtyl9y3vRJA3r3wZknSCT7G7yHWhWgEOK/vPm4DZuNPq8 X-MS-Exchange-Transport-CrossTenantHeadersStamped: PA4PR08MB6238 Original-Authentication-Results: sourceware.org; dkim=none (message not signed) header.d=none; sourceware.org; dmarc=none action=none header.from=arm.com; X-EOPAttributedMessage: 0 X-MS-Exchange-Transport-CrossTenantHeadersStripped: AM5EUR03FT026.eop-EUR03.prod.protection.outlook.com X-MS-Office365-Filtering-Correlation-Id-Prvs: 441592dc-8275-4f90-f2d8-08d8bbcd7ef7 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: YOFpEWrzVf1JLY/+oB09zPEajuZhRYWUQQQZ2ba3NlGx9M1LDJ3vlQ8+MIowyUewpNg65SLrtiaL9NiaeEFeoDGXTEYv1wPYo1p3XQjKkJEHBbQsUgOWwlKP4GBVjMGwHrR91O/W5CHj4WoXxIxJ8ynYtBbw3TNN5rmGPXNhzJzkIEQCQ4QfzwC2uisawxzRamodAF7OUlMfKM7Q3mxfM5jZf1J+9cHJfUW6UJ5k3moZxgB/dPDFbxREeUfh1g1pKhgBpnKXQ9wd3T1IOZfiUkf3qpnPcjYsQLgXl1z6Y6mvMj54kKq2XNdihw0R4tKKUrmVHac4/xTVNj00FNoNM9uxSKiVBJa6J0H2pAxE3JsYpJqlzB7JBayxt1STSPaWPLgBwcPZ5psRbCaPDcUoz5xuM6gc0Dg1XFpiAPjnt/1KmxHrmryiGTbRoMmQwQNj0frNpDkUiG3J86bnEoOdPbgr08z+V3FyQnSJ57zTJuhheSp/nnaK0zL9ENHQs2Qm X-Forefront-Antispam-Report: CIP:63.35.35.123; CTRY:IE; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:64aa7808-outbound-1.mta.getcheckrecipient.com; PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com; CAT:NONE; SFS:(4636009)(346002)(376002)(136003)(39860400002)(396003)(46966006)(70206006)(336012)(356005)(70586007)(26005)(86362001)(956004)(44832011)(83380400001)(81166007)(36756003)(478600001)(8936002)(6916009)(2616005)(47076005)(6666004)(16526019)(186003)(82740400003)(82310400003)(5660300002)(69590400011)(2906002)(8676002)(34010700045)(316002)(6486002)(6506007)(6512007); DIR:OUT; SFP:1501; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2021 16:24:40.7260 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 1b370800-50c8-425a-eab9-08d8bbcd8e96 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d; Ip=[63.35.35.123]; Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: AM5EUR03FT026.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3926 X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, MSGID_FROM_MTA_HEADER, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP, UNPARSEABLE_RELAY 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-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Szabolcs Nagy via Libc-alpha From: Szabolcs Nagy Reply-To: Szabolcs Nagy Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" With static pie linking pointers in the tunables list need RELATIVE relocs since the absolute address is not known at link time. We want to avoid relocations so the static pie self relocation can be done after tunables are initialized. This is a simple fix that embeds the tunable strings into the tunable list instead of using pointers. It is possible to have a more compact representation of tunables with some additional complexity in the generator and tunable parser logic. Such optimization will be useful if the list of tunables grows. There is still an issue that tunables_strdup allocates and the failure handling code path is sufficiently complex that it can easily have RELATIVE relocations. It is possible to avoid the early allocation and only change environment variables in a setuid exe after relocations are processed. But that is a bigger change and early failure is fatal anyway so it is not as critical to fix right away. This is bug 27181. Reviewed-by: Adhemerval Zanella --- elf/dl-tunable-types.h | 4 ++-- elf/dl-tunables.c | 2 +- scripts/gen-tunables.awk | 12 +++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/elf/dl-tunable-types.h b/elf/dl-tunable-types.h index 05d4958e1c..3fcc0806f5 100644 --- a/elf/dl-tunable-types.h +++ b/elf/dl-tunable-types.h @@ -59,7 +59,7 @@ typedef enum /* A tunable. */ struct _tunable { - const char *name; /* Internal name of the tunable. */ + const char name[TUNABLE_NAME_MAX]; /* Internal name of the tunable. */ tunable_type_t type; /* Data type of the tunable. */ tunable_val_t val; /* The value. */ bool initialized; /* Flag to indicate that the tunable is @@ -75,7 +75,7 @@ struct _tunable target module if the value is considered unsafe. */ /* Compatibility elements. */ - const char *env_alias; /* The compatibility environment + const char env_alias[TUNABLE_ALIAS_MAX]; /* The compatibility environment variable name. */ }; diff --git a/elf/dl-tunables.c b/elf/dl-tunables.c index 33be00e447..e44476f204 100644 --- a/elf/dl-tunables.c +++ b/elf/dl-tunables.c @@ -351,7 +351,7 @@ __tunables_init (char **envp) /* Skip over tunables that have either been set already or should be skipped. */ - if (cur->initialized || cur->env_alias == NULL) + if (cur->initialized || cur->env_alias[0] == '\0') continue; const char *name = cur->env_alias; diff --git a/scripts/gen-tunables.awk b/scripts/gen-tunables.awk index cda12ef62e..fa63e86d1a 100644 --- a/scripts/gen-tunables.awk +++ b/scripts/gen-tunables.awk @@ -12,6 +12,8 @@ BEGIN { tunable="" ns="" top_ns="" + max_name_len=0 + max_alias_len=0 } # Skip over blank lines and comments. @@ -57,11 +59,14 @@ $1 == "}" { maxvals[top_ns,ns,tunable] = max_of[types[top_ns,ns,tunable]] } if (!env_alias[top_ns,ns,tunable]) { - env_alias[top_ns,ns,tunable] = "NULL" + env_alias[top_ns,ns,tunable] = "{0}" } if (!security_level[top_ns,ns,tunable]) { security_level[top_ns,ns,tunable] = "SXID_ERASE" } + len = length(top_ns"."ns"."tunable) + if (len > max_name_len) + max_name_len = len tunable = "" } @@ -109,6 +114,9 @@ $1 == "}" { } else if (attr == "env_alias") { env_alias[top_ns,ns,tunable] = sprintf("\"%s\"", val) + len = length(val) + if (len > max_alias_len) + max_alias_len = len } else if (attr == "security_level") { if (val == "SXID_ERASE" || val == "SXID_IGNORE" || val == "NONE") { @@ -158,6 +166,8 @@ END { print "\n#ifdef TUNABLES_INTERNAL" # Internal definitions. + print "# define TUNABLE_NAME_MAX " (max_name_len + 1) + print "# define TUNABLE_ALIAS_MAX " (max_alias_len + 1) print "# include \"dl-tunable-types.h\"" # Finally, the tunable list. print "static tunable_t tunable_list[] attribute_relro = {"