Skip to content

Commit ddc6597

Browse files
joshtriplettakpm00
authored andcommitted
prctl: add PR_GET_AUXV to copy auxv to userspace
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 <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent 97f7e09 commit ddc6597

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

include/uapi/linux/prctl.h

+2
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,6 @@ struct prctl_mm_map {
290290
#define PR_SET_VMA 0x53564d41
291291
# define PR_SET_VMA_ANON_NAME 0
292292

293+
#define PR_GET_AUXV 0x41555856
294+
293295
#endif /* _LINUX_PRCTL_H */

kernel/sys.c

+15
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,16 @@ static inline int prctl_get_mdwe(unsigned long arg2, unsigned long arg3,
23882388
PR_MDWE_REFUSE_EXEC_GAIN : 0;
23892389
}
23902390

2391+
static int prctl_get_auxv(void __user *addr, unsigned long len)
2392+
{
2393+
struct mm_struct *mm = current->mm;
2394+
unsigned long size = min_t(unsigned long, sizeof(mm->saved_auxv), len);
2395+
2396+
if (size && copy_to_user(addr, mm->saved_auxv, size))
2397+
return -EFAULT;
2398+
return sizeof(mm->saved_auxv);
2399+
}
2400+
23912401
SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
23922402
unsigned long, arg4, unsigned long, arg5)
23932403
{
@@ -2518,6 +2528,11 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
25182528
else
25192529
return -EINVAL;
25202530
break;
2531+
case PR_GET_AUXV:
2532+
if (arg4 || arg5)
2533+
return -EINVAL;
2534+
error = prctl_get_auxv((void __user *)arg2, arg3);
2535+
break;
25212536
default:
25222537
return -EINVAL;
25232538
}

0 commit comments

Comments
 (0)