Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp6446595rwl; Tue, 4 Apr 2023 12:55:22 -0700 (PDT) X-Google-Smtp-Source: AKy350bbvsaRELyCpQPaSiiHZQS//HchOpAb7veMH4Wl+uQAMMsSsIhZZrqUdyAoKn6lkC5K5oL1 X-Received: by 2002:a05:6402:756:b0:502:1cae:8b11 with SMTP id p22-20020a056402075600b005021cae8b11mr637448edy.23.1680638122462; Tue, 04 Apr 2023 12:55:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680638122; cv=none; d=google.com; s=arc-20160816; b=E56bGr1Z+mRjImbWQ8ploMLd4FiAdGOAtoBOZXsiL3CIF4jGT+pTMhjxO7oJA3GfDw jACJB2qVCTW5yojdiiC9AU3sHSejzs/x0CmnUjgg1emrX0ziQ3dcS3zxGmpVsRV3t2/U k8nBvzOUruk9Nx3udgbHLn1uLB+lUaSaf6+ppR4lVkY5sPMbcKXXsF9WKd/TLZP5JO7D q2JPh1VXpK8JhDVsqNdFMLH+PqU8bUg9NdQVzdzmf3atXLpizrSlbKMkBIFZF3GV1vDT jeTVnOXSh7fQjOJAA6C8l027E1elvcqPOq+MEz2Fv3NuZo+yLv8aKF6MAhWmPldeQz4R gTTg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=CidfPN/6eMDMLYBvzMJ84Tx+lakBgATy6jtQSUZUNk0=; b=fdgfXPB7ezech/RI2ZBgCn8GabypkZuvIWlQyhspRegp20WUJA4enlqxwpx+vsIl1d YQ5CLJJkhiCtqVAVXaNMIOPOAs7uaNXcveOF0UUg7Rx1+Q2aDagGlFgI7UUWv9M/A/5o uPGkfreYGL+u9WoesY47vGK7vucUW70Brk/rO05lV+C8Tm7d6SdGQ9ac0gzwJWruOBZq /qKdt3gIqcWcxc8HvcDUuheS6j+4dbO+ZmVebXHz7ElLFyWk0UmmUms/Ut//MRtYPY04 K/rFubVDPK7q6IsgD/jbdG/M4Qo7S1fo8Aa0p1WdJa/JdaERiLppvJWzEO52Soofyoyz KtUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux-foundation.org header.s=korg header.b=u3JeDRjE; 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 k24-20020aa7d8d8000000b005029459a6c5si2356870eds.365.2023.04.04.12.54.57; Tue, 04 Apr 2023 12:55:22 -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=@linux-foundation.org header.s=korg header.b=u3JeDRjE; 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 S236161AbjDDToB (ORCPT + 99 others); Tue, 4 Apr 2023 15:44:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235905AbjDDTn7 (ORCPT ); Tue, 4 Apr 2023 15:43:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0FD8E4EE9 for ; Tue, 4 Apr 2023 12:43:57 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 5569D63062 for ; Tue, 4 Apr 2023 19:43:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0228C433EF; Tue, 4 Apr 2023 19:43:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1680637436; bh=1Pj9wervIvuhxnbud9M7uEnumbv+qewHT7HtKpfIDE0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=u3JeDRjED8NfLSy7JgtnzRWXs8mnHtoxwLXgKI8TrOSi/KA99r+V3s3xMiq5k29wc mM2t1mpsctRZyI25Rxtg1sRpN6GavOMHoh071IbNwPezCuQS7D8+tQi8YKcwGiKHMy 3Z27iTHQOYUlsTYsSxers71SWSjBt2yrcMul/pNo= Date: Tue, 4 Apr 2023 12:43:55 -0700 From: Andrew Morton To: Josh Triplett Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] prctl: Add PR_GET_AUXV to copy auxv to userspace Message-Id: <20230404124355.3efa7a201d2aa9896dc6353e@linux-foundation.org> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-7.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,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, 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? > Add a prctl that copies current->mm->saved_auxv to a userspace buffer. > > ... > > --- 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? > --- 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. > 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;