prctl: add PR_GET_AUXV to copy auxv to userspace
authorJosh Triplett <josh@joshtriplett.org>
Tue, 4 Apr 2023 12:31:48 +0000 (21:31 +0900)
committerAndrew Morton <akpm@linux-foundation.org>
Wed, 5 Apr 2023 23:02:57 +0000 (16:02 -0700)
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.

Link: https://lkml.kernel.org/r/d81864a7f7f43bca6afa2a09fc2e850e4050ab42.1680611394.git.josh@joshtriplett.org
Signed-off-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
include/uapi/linux/prctl.h
kernel/sys.c

index 759b3f53e53f02373e90e4eb00ffec92f4c4802d..32b5c739aaedf279dfca93d5fb89b31d1e4d1201 100644 (file)
@@ -292,4 +292,7 @@ struct prctl_mm_map {
 
 #define PR_SET_MEMORY_MERGE            67
 #define PR_GET_MEMORY_MERGE            68
+
+#define PR_GET_AUXV                    0x41555856
+
 #endif /* _LINUX_PRCTL_H */
index 25131dabaaa1f4e3ff885b174236c9b6f1d7d274..efc559813e46d655d1ea33c4fb41440b8e87dcb1 100644 (file)
@@ -2389,6 +2389,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);
+}
+
 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
                unsigned long, arg4, unsigned long, arg5)
 {
@@ -2519,6 +2529,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
                        else
                                return -EINVAL;
                        break;
+       case PR_GET_AUXV:
+               if (arg4 || arg5)
+                       return -EINVAL;
+               error = prctl_get_auxv((void __user *)arg2, arg3);
+               break;
                default:
                        return -EINVAL;
                }