Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp3788rwl; Tue, 4 Apr 2023 17:43:17 -0700 (PDT) X-Google-Smtp-Source: AKy350ZxCU/oq1VqGcXdltiHa8QDkKa4XR9C11yW5CglYTuDSvrAACUI6xFv1T4SGNSBXylxTfU3 X-Received: by 2002:a05:6a20:bb16:b0:d9:a792:8e3d with SMTP id fc22-20020a056a20bb1600b000d9a7928e3dmr3582400pzb.30.1680655396777; Tue, 04 Apr 2023 17:43:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680655396; cv=none; d=google.com; s=arc-20160816; b=ZmTvVLwnQgBPT+iDVD4phDNQp35FgVWX7DwbeRwpw3Jvomv6ObN1JwKVb8xtDVcRs3 O+VgelRCnXkanXHaDtUkOvpGNCuLYYxURhJyJaP3o8JMwvI6gYCuhgxzYUc62jWBZKKZ aeexbe+EoY3UtkKqPj8l9Q5kYl4sYo3u/F2YUj8TJLmtM3cvChD59On2sUoANjN88UcX nljQ+ssf73JJtT20xNla4j+mTGoGAPzJ5AM1caNxeIm02wBzv4viQVe1hoBCLg8x8mW7 XtducjjKcPLO4KJArZsmrih8AEochZh0DDheO9F1m4w9VNaTLZn5jWrUZn+UqDdYXLgN kiow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:to:from:date:feedback-id:dkim-signature:dkim-signature; bh=ejzhtkUjzuEOBfr+jkw68scBu5KVz3rG4G/vQRTMeww=; b=pJefJTwHJWgptABZ8sUBnSuOQOYMaFZ6xPjoUm74p42l7CJoIm9S2q3nKHiC+qpaTt XYN+A0TR61UAxIOaIWwlfQVxr2/0P4H2CTKyC1q8/R89itMQCSpa2g+ec6YChDlxal5t VuvrPuM/7kl87XCmdplHfIK3afAjvNCjrnwsLpVOKLptCpEq4UkG5d2ZmuDvmRRhw/+f EQWRVk7nX8NKZL5PSn4n1vXw8w+7iOnv4jppi/fgQuXrVOj0Rl8xDTjVsrSSL7sYeqBM 7wANkGxM3YyLvnlkQBfAZJ8w5uph+49rlSK3GvmREqVeoFOojOMqsjiVU1tkppUWJaVO FY8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joshtriplett.org header.s=fm1 header.b=VrHN7PaO; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=isrBH6eF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 7-20020a630507000000b0050bf5235c2fsi11214477pgf.509.2023.04.04.17.42.59; Tue, 04 Apr 2023 17:43:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@joshtriplett.org header.s=fm1 header.b=VrHN7PaO; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=isrBH6eF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236386AbjDEAiV (ORCPT + 99 others); Tue, 4 Apr 2023 20:38:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234892AbjDEAiU (ORCPT ); Tue, 4 Apr 2023 20:38:20 -0400 Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8666D1705 for ; Tue, 4 Apr 2023 17:38:19 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id EDAD93200A78; Tue, 4 Apr 2023 20:38:18 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Tue, 04 Apr 2023 20:38:19 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= joshtriplett.org; h=cc:content-type:content-type:date:date:from :from:in-reply-to:message-id:mime-version:reply-to:sender :subject:subject:to:to; s=fm1; t=1680655098; x=1680741498; bh=ej zhtkUjzuEOBfr+jkw68scBu5KVz3rG4G/vQRTMeww=; b=VrHN7PaOMJ/lYHLRBV JLadOyWW2FWqqZR3LmQCOIw34A9FM2Omrk/7VOVGOx4uXtefv9yCPdaL2wHAsclP UGKyYs5j7Uakggiw5qPaMxrs6zBQDtWU/3l7bBXfxWqtp6FsUMr3wBqlMmMRxBJd 5swd1rMVnwY4EkPk//l9BO+nvWGe9WnZ8L7eN0rdRgQiDP1aeKWKTEEFnzOJN/bq 85o0VX7kbkUkOsWKYKQp05hB1GdyWMRNdTptdqtrUSCGr56RkYHVBWeeQMRSK7lf 4JmfqfiXIAvXy12arIDoFSsFpcdl2ks5CCJh0JKHnl1z4iYaMWTsgCehZlUF+lFj 4YvA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:message-id :mime-version:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1680655098; x=1680741498; bh=ejzhtkUjzuEOBfr+jkw68scBu5KVz3rG4G/ vQRTMeww=; b=isrBH6eFfRH4sgMtk+URn+vOWJWB9zZ77RVq67miD5qIdyUG4tq iNurQ9hCw8RUaZQM8zvyvrIOitXCXa+7EqfAwUI7yNs4Xu7IXyFvFviglmCTx6bJ ccURZXKRrNkDGHFe90U4eF1l7DtC4Qe4xYQlr/YI0fHeL2h+SuNzMCzCyPs6kI0D 03fgXKcN+EFwPIrM+tdipadSGnfK5SDtLxYtyANpdB/2aXA/umIPlIEviUNh9Q6N B8+EFJJ9giSXwginI9IWbY8UnEsmmNqYlBZz4X0eDOkLBGNxPomCF24KcAC/WT89 yiyQ+86R9u/ZGjlxNAKQNln3Gtmm+WD/tKQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdejtddgfeegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvuffkgggtugesthdtredttd dtvdenucfhrhhomheplfhoshhhucfvrhhiphhlvghtthcuoehjohhshhesjhhoshhhthhr ihhplhgvthhtrdhorhhgqeenucggtffrrghtthgvrhhnpeelleeggedtjeejfeeuvddufe eggfektdefkeehveeuvedvvdfhgeffgfdvgfffkeenucevlhhushhtvghrufhiiigvpedt necurfgrrhgrmhepmhgrihhlfhhrohhmpehjohhshhesjhhoshhhthhrihhplhgvthhtrd horhhg X-ME-Proxy: Feedback-ID: i83e94755:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 4 Apr 2023 20:38:16 -0400 (EDT) Date: Wed, 5 Apr 2023 09:38:14 +0900 From: Josh Triplett To: Andrew Morton , linux-kernel@vger.kernel.org Subject: [PATCH v3] prctl: Add PR_GET_AUXV to copy auxv to userspace Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a library wants to get information from auxv (for instance, AT_HWCAP/AT_HWCAP2), it has a few options, none of them perfectly reliable or ideal: - Be main or the pre-main startup code, and grub through the stack above main. Doesn't work for a library. - Call libc getauxval. Not ideal for libraries that are trying to be libc-independent and/or don't otherwise require anything from other libraries. - Open and read /proc/self/auxv. Doesn't work for libraries that may run in arbitrarily constrained environments that may not have /proc mounted (e.g. libraries that might be used by an init program or a container setup tool). - Assume you're on the main thread and still on the original stack, and try to walk the stack upwards, hoping to find auxv. Extremely bad idea. - Ask the caller to pass auxv in for you. Not ideal for a user-friendly library, and then your caller may have the same problem. Add a prctl that copies current->mm->saved_auxv to a userspace buffer. Signed-off-by: Josh Triplett --- v2: - Fix W=1 warning about min type mismatch v3: - Change argument type to size_t. (Left the return value as int to match all the other helpers in the file.) I've built, booted, and tested this. man-pages snippet: .\" prctl PR_GET_AUXV .TP .BR PR_GET_AUXV " (since Linux 6.x)" Get the auxilliary vector (auxv) into the buffer pointed to by .IR "(void\~*) arg2" , whose length is given by \fIarg3\fP. If the buffer is not long enough for the full auxilliary vector, the copy will be truncated. Return (as the function result) the full length of the auxilliary vector. \fIarg4\fP and \fIarg5\fP must be 0. Will send a patch for man-pages once merged. include/uapi/linux/prctl.h | 2 ++ kernel/sys.c | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/include/uapi/linux/prctl.h b/include/uapi/linux/prctl.h index 1312a137f7fb..b2b24eaf2427 100644 --- a/include/uapi/linux/prctl.h +++ b/include/uapi/linux/prctl.h @@ -290,4 +290,6 @@ struct prctl_mm_map { #define PR_SET_VMA 0x53564d41 # define PR_SET_VMA_ANON_NAME 0 +#define PR_GET_AUXV 0x41555856 + #endif /* _LINUX_PRCTL_H */ diff --git a/kernel/sys.c b/kernel/sys.c index 495cd87d9bf4..f031edb8bc47 100644 --- a/kernel/sys.c +++ b/kernel/sys.c @@ -2377,6 +2377,16 @@ static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3, PR_MDWE_REFUSE_EXEC_GAIN : 0; } +static int prctl_get_auxv(void __user *addr, size_t len) +{ + struct mm_struct *mm = current->mm; + size_t size = min_t(size_t, sizeof(mm->saved_auxv), len); + + if (size && copy_to_user(addr, mm->saved_auxv, size)) + return -EFAULT; + return sizeof(mm->saved_auxv); +} + SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, unsigned long, arg4, unsigned long, arg5) { @@ -2661,6 +2671,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, case PR_SET_VMA: error = prctl_set_vma(arg2, arg3, arg4, arg5); break; + case PR_GET_AUXV: + if (arg4 || arg5) + return -EINVAL; + error = prctl_get_auxv((void __user *)arg2, arg3); + break; default: error = -EINVAL; break; -- 2.40.0