Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6688916rwl; Tue, 4 Apr 2023 17:32:23 -0700 (PDT) X-Google-Smtp-Source: AKy350aLJV0X3YmNhwA+MwpKMoh6S2zZS2MSr90Lq0KLu7VSLWXb/h4cYMkYgRrh7eT1DkuAiaeZ X-Received: by 2002:a17:906:b25a:b0:931:7350:a4f3 with SMTP id ce26-20020a170906b25a00b009317350a4f3mr1232900ejb.10.1680654743477; Tue, 04 Apr 2023 17:32:23 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680654743; cv=none; d=google.com; s=arc-20160816; b=bPkM17kv9UwndMBKBIkXP69ad/78GXNEj/XA1ffAwdR88NCtKflpzLSknVdgwRGUVM aJ6S1sysRw3Jwr8iA7z/hg9bzkx1sHSta5mZshf7Yn0nV6GQinlHzm27/JR01gc+sxTv lwhs9XbZ8Z6qhd91O8D9MlRylbkl4kQG2vPzuHsOQYw1Hk3v4pUVEXV4VD9ZpRvatTq6 6KXyONc/9sViK+7nBsbykK00xy5oxKp/EqrJ3ycFTcQebEDgOgtwzJNGjEcY2YyKnr2C qtF01ZVzO9lyZ1Oxi1AjUil+Ek1vuzQUQ77zusY7dpVupxqbwGOxFTlq3VCoEwipwzbg GfaA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:feedback-id :dkim-signature:dkim-signature; bh=Mx+3ceIjpuMWUpIMcp9gQXCLaSq9TOOpb+JxGIo/9cA=; b=T6tYri4Lq4m+JfxZtgp91U59a9wmP0g7/M0srFbWWVToAZtQ+bH7hMecKj2tCoQAK/ zyeazWYtgNI+RvzdaDTTjpm2RJFYcA2E0HgC6pudgS0IWBSKKh4H5lXEzVyQHiT2YTg5 KAcYO+ywzfGGmhr7oHmu1YfivoiHhWY1yBm3nNdmBvV1RIo/LbeHI0QAZ5XMfp/0EIXZ iElZEVGVYTKt41sbhVk1vVpRUpKtwzIHm5Of9gHf6+HGwWO1DUNIYQ3Uf+HvJtpQdPrT 6X4eE8SKbG2PMIlFH/DepsK/N7wE4d3yVzh06jPGV2uJ1jtNBPtwzNcmkqJJHHWMY9/w RW0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@joshtriplett.org header.s=fm1 header.b=CE85zjxh; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=K2usp+SX; 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 j10-20020a50ed0a000000b004fbbd7bee7csi10891176eds.58.2023.04.04.17.31.49; Tue, 04 Apr 2023 17:32:23 -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=CE85zjxh; dkim=pass header.i=@messagingengine.com header.s=fm2 header.b=K2usp+SX; 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 S234955AbjDEAY5 (ORCPT + 99 others); Tue, 4 Apr 2023 20:24:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236551AbjDEAYl (ORCPT ); Tue, 4 Apr 2023 20:24:41 -0400 Received: from wout4-smtp.messagingengine.com (wout4-smtp.messagingengine.com [64.147.123.20]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31EEF4697 for ; Tue, 4 Apr 2023 17:24:38 -0700 (PDT) Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id CC49332009C8; Tue, 4 Apr 2023 20:24:35 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Tue, 04 Apr 2023 20:24:36 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= joshtriplett.org; h=cc:cc:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:sender:subject:subject:to:to; s=fm1; t= 1680654275; x=1680740675; bh=Mx+3ceIjpuMWUpIMcp9gQXCLaSq9TOOpb+J xGIo/9cA=; b=CE85zjxhK1DPeU2JCKYp4z2uFnTO9DaVAvDgttFVg8mbxqbRxiA DPy5XLJAZ3/snXYkXfx78pLUrlQHqK2FnN9hGQ2ft8aCRjTLwmTxIFsTYQ0TmZzC 92C+Qk5Q0gyP2pgORJIrY9K/EQ4sVw7IKK9KkCk4oA1tAlaAnmWIF9QHiJCTdrEY 5d8AnZr6kxSWcewAJlEotTZKz7SL01cbdRszVPeDJESxQNhdGRU0TBC0aOEvTmN0 RUOWk5mjx+rP7vlwrll/FRQCQTmJth7hDckNWFzLJeKuQ6eIf8c/L8yOoCRSOg0d 45YQdchFmJNC4sIU0FKikgubaPROC0qtwuA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-type:content-type:date:date :feedback-id:feedback-id:from:from:in-reply-to:in-reply-to :message-id:mime-version:references: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=1680654275; x=1680740675; bh=Mx+3ceIjpuMWU pIMcp9gQXCLaSq9TOOpb+JxGIo/9cA=; b=K2usp+SXX4/YOmEgHiHEjRvy/boeX WrwruqLDVAG/EGZBsB2NK77LzL1xMw2X42QDYbyzsTQzv7OqLV7LnQMcL0ne6XdH 67QrSaiGhyOizctyR5m9t0Mmhcu0BXUnBMRtbuYTgZdjpk6Pp0DLbT2NDdNS2F2b b8hTUOzhe6z80V4RFasnIWSuetIZLfoaHC4j2wqN51tG9ZdVdn9xbzlWAkHXp0hW 0rHUZuUyLSF9wS82xxRAz3Lqw4yNx8f8rhvV6zQegHeCtgRZ8K/5xMb1UN+/tkh3 /YEeGb9oYZ+mKCoeHnmrA7CVymGdHWczfzL7VC+w8fSMHgetYZXsQso2g== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdejtddgfeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucenucfjughrpeffhffvvefukfhfgggtuggjsehttd ertddttddvnecuhfhrohhmpeflohhshhcuvfhrihhplhgvthhtuceojhhoshhhsehjohhs hhhtrhhiphhlvghtthdrohhrgheqnecuggftrfgrthhtvghrnhepudeigeehieejuedvte dufeevtdejfeegueefgffhkefgleefteetledvtdfftefgnecuvehluhhsthgvrhfuihii vgeptdenucfrrghrrghmpehmrghilhhfrhhomhepjhhoshhhsehjohhshhhtrhhiphhlvg htthdrohhrgh X-ME-Proxy: Feedback-ID: i83e94755:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 4 Apr 2023 20:24:33 -0400 (EDT) Date: Wed, 5 Apr 2023 09:24:30 +0900 From: Josh Triplett To: Andrew Morton Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] prctl: Add PR_GET_AUXV to copy auxv to userspace Message-ID: References: <20230404124355.3efa7a201d2aa9896dc6353e@linux-foundation.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20230404124355.3efa7a201d2aa9896dc6353e@linux-foundation.org> 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 On Tue, Apr 04, 2023 at 12:43:55PM -0700, Andrew Morton wrote: > On Tue, 4 Apr 2023 21:31:48 +0900 Josh Triplett wrote: > > > 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. > > How does glibc's getauxval() do its thing? Why can't glibc-independent > code do the same thing? glibc owns the pre-main startup code in programs linked to glibc, so it can record auxv for later reference in getauxval. That isn't an option for something that *doesn't* own the pre-main startup code. > > --- 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 > > How was this constant arrived at? It's 'A' 'U' 'X' 'V', inspired by PR_SET_VMA above which is 'S' 'V' 'M' 'A'. > > --- 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, unsigned long len) > > +{ > > + struct mm_struct *mm = current->mm; > > + unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len); > > + > > + if (size && copy_to_user(addr, mm->saved_auxv, size)) > > + return -EFAULT; > > + return sizeof(mm->saved_auxv); > > +} > > The type choices are unpleasing. Maybe make `len' a size_t and make > the function return a size_t? That way prctl_get_auxv() will be much > nicer, but the caller less so. It'd have to be an ssize_t return to support returning -EFAULT. Also, sadly, size_t would still look just as bad, because `sizeof(mm->saved_auxv)` doesn't have type size_t (at least according to the error from the type-safe min macro). So this would still need a cast or a `min_t`. But I'm happy to change the argument to size_t and the return value to ssize_t, if you'd prefer. Will send v3 with that changed. - Josh Triplett