syscalls,x86: Add execveat() system call
Subsystems (10)
v5 cover syscalls,x86: Add execveat() system call
This patch set adds execveat(2) for x86, and is derived from Meredydd Luff's patch from Sept 2012 (https://lkml.org/lkml/2012/9/11/528).
The primary aim of adding an execveat syscall is to allow an implementation of fexecve(3) that does not rely on the /proc filesystem. The current glibc version of fexecve(3) is implemented via /proc, which causes problems in sandboxed or otherwise restricted environments.
Given the desire for a /proc-free fexecve() implementation, HPA suggested (https://lkml.org/lkml/2006/7/11/556) that an execveat(2) syscall would be an appropriate generalization.
Also, having a new syscall means that it can take a flags argument without back-compatibility concerns. The current implementation just defines the AT_EMPTY_PATH and AT_SYMLINK_NOFOLLOW flags, but other flags could be added in future -- for example, flags for new namespaces (as suggested at https://lkml.org/lkml/2006/7/11/474).
Related history:
- https://lkml.org/lkml/2006/12/27/123 is an example of someone realizing that fexecve() is likely to fail in a chroot environment.
- http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=514043 covered documenting the /proc requirement of fexecve(3) in its manpage, to "prevent other people from wasting their time".
- https://bugzilla.kernel.org/show_bug.cgi?id=74481 documented that it's not possible to fexecve() a file descriptor for a script with close-on-exec set (which is possible with the implementation here).
- https://bugzilla.redhat.com/show_bug.cgi?id=241609 described a problem where a process that did setuid() could not fexecve() because it no longer had access to /proc/self/fd; this has since been fixed.
Changes since v4, suggested by Eric W. Biederman:
- Use empty filename with AT_EMPTY_PATH flag rather than NULL pathname to request fexecve-like behaviour.
- Build pathname as "/dev/fd/<fd>/<filename>" (or "/dev/fd/<fd>") rather than using d_path().
- Patch against v3.17 (bfe01a5ba249)
Changes since Meredydd's v3 patch:
- Added a selftest.
- Added a man page.
- Left open_exec() signature untouched to reduce patch impact elsewhere (as suggested by Al Viro).
- Filled in bprm->filename with d_path() into a buffer, to avoid use of potentially-ephemeral dentry->d_name.
- Patch against v3.14 (455c6fdbd21916).
David Drysdale (2):
syscalls,x86: implement execveat() system call
syscalls,x86: add selftest for execveat(2)
arch/x86/ia32/audit.c | 1 + arch/x86/ia32/ia32entry.S | 1 + arch/x86/kernel/audit_64.c | 1 + arch/x86/kernel/entry_64.S | 28 +++ arch/x86/syscalls/syscall_32.tbl | 1 + arch/x86/syscalls/syscall_64.tbl | 2 + arch/x86/um/sys_call_table_64.c | 1 + fs/exec.c | 130 ++++++++++++-- fs/namei.c | 2 +- include/linux/compat.h | 3 + include/linux/fs.h | 1 + include/linux/sched.h | 4 + include/linux/syscalls.h | 4 + include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 3 + lib/audit.c | 3 + tools/testing/selftests/Makefile | 1 + tools/testing/selftests/exec/.gitignore | 6 + tools/testing/selftests/exec/Makefile | 22 +++ tools/testing/selftests/exec/execveat.c | 295 ++++++++++++++++++++++++++++++++ 20 files changed, 497 insertions(+), 16 deletions(-) create mode 100644 tools/testing/selftests/exec/.gitignore create mode 100644 tools/testing/selftests/exec/Makefile create mode 100644 tools/testing/selftests/exec/execveat.c
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH v5 1/3 syscalls,x86: implement execveat() system call
Add a new system execveat(2) syscall. execveat() is to execve() as openat() is to open(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.
In addition, if the filename is empty and AT_EMPTY_PATH is specified, execveat() executes the file to which the file descriptor refers. This replicates the functionality of fexecve(), which is a system call in other UNIXen, but in Linux glibc it depends on opening "/proc/self/fd/<fd>" (and so relies on /proc being mounted).
The filename fed to the executed program as argv[0] (or the name of the script fed to a script interpreter) will be of the form "/dev/fd/<fd>" (for an empty filename) or "/dev/fd/<fd>/<filename>", effectively reflecting how the executable was found. This does however mean that execution of a script in a /proc-less environment won't work.
Only x86-64, i386 and x32 ABIs are supported in this patch.
Based on patches by Meredydd Luff <[email protected]>
--- arch/x86/ia32/audit.c | 1 + arch/x86/ia32/ia32entry.S | 1 + arch/x86/kernel/audit_64.c | 1 + arch/x86/kernel/entry_64.S | 28 ++++++++ arch/x86/syscalls/syscall_32.tbl | 1 + arch/x86/syscalls/syscall_64.tbl | 2 + arch/x86/um/sys_call_table_64.c | 1 + fs/exec.c | 130 ++++++++++++++++++++++++++++++++++---- fs/namei.c | 2 +- include/linux/compat.h | 3 + include/linux/fs.h | 1 + include/linux/sched.h | 4 ++ include/linux/syscalls.h | 4 ++ include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 3 + lib/audit.c | 3 + 16 files changed, 173 insertions(+), 16 deletions(-)
arch/x86/ia32/audit.c +1 -0
@@ -35,6 +35,7 @@ int ia32_classify_syscall(unsigned syscall)case __NR_socketcall:return 4 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 1 ;
arch/x86/ia32/ia32entry.S +1 -0
@@ -464,6 +464,7 @@ GLOBAL(\label) PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn PTREGSCALL stub32_sigreturn, sys32_sigreturn PTREGSCALL stub32_execve, compat_sys_execve + PTREGSCALL stub32_execveat, compat_sys_execveat PTREGSCALL stub32_fork, sys_fork PTREGSCALL stub32_vfork, sys_vfork
arch/x86/kernel/audit_64.c +1 -0
@@ -50,6 +50,7 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_openat:return 3 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 0 ;
arch/x86/kernel/entry_64.S +28 -0
@@ -665,6 +665,20 @@ ENTRY(stub_execve) CFI_ENDPROC END(stub_execve) +ENTRY(stub_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_execveat) + /* * sigreturn is special because it needs to restore all registers on return. * This cannot be done with SYSRET, so use the IRET return path instead.
@@ -710,6 +724,20 @@ ENTRY(stub_x32_execve) CFI_ENDPROC END(stub_x32_execve) +ENTRY(stub_x32_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call compat_sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_x32_execveat) + #endif /*
arch/x86/syscalls/syscall_32.tbl +1 -0
@@ -363,3 +363,4 @@ 354 i386 seccomp sys_seccomp 355 i386 getrandom sys_getrandom 356 i386 memfd_create sys_memfd_create +357 i386 execveat sys_execveat stub32_execveat
arch/x86/syscalls/syscall_64.tbl +2 -0
@@ -327,6 +327,7 @@ 318 common getrandom sys_getrandom 319 common memfd_create sys_memfd_create 320 common kexec_file_load sys_kexec_file_load +321 64 execveat stub_execveat # # x32-specific system call numbers start at 512 to avoid cache impact
@@ -365,3 +366,4 @@ 542 x32 getsockopt compat_sys_getsockopt 543 x32 io_setup compat_sys_io_setup 544 x32 io_submit compat_sys_io_submit +545 x32 execveat stub_x32_execveat
arch/x86/um/sys_call_table_64.c +1 -0
@@ -31,6 +31,7 @@#define stub_fork sys_fork#define stub_vfork sys_vfork#define stub_execve sys_execve +#define stub_execveat sys_execveat#define stub_rt_sigreturn sys_rt_sigreturn#define __SYSCALL_COMMON (nr, sym, compat) __SYSCALL_64(nr, sym, compat)
fs/exec.c +116 -14
206 lines not on this page · raw message
fs/namei.c +1 -1
@@ -130,7 +130,7 @@ void final_putname(struct filename *name)#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) -static struct filename * +struct filename *getname_flags (const char __user *filename,int flags,int *empty) {struct filename *result, *err;
include/linux/compat.h +3 -0
@@ -357,6 +357,9 @@ asmlinkage long compat_sys_lseek(unsigned int, compat_off_t, unsigned int);asmlinkage long compat_sys_execve (const char __user *filename,const compat_uptr_t __user *argv,const compat_uptr_t __user *envp); +asmlinkage long compat_sys_execveat (int dfd,const char __user *filename, +const compat_uptr_t __user *argv, +const compat_uptr_t __user *envp,int flags); asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp, compat_ulong_t __user *exp,
include/linux/fs.h +1 -0
@@ -2060,6 +2060,7 @@ extern struct file *file_open_root(struct dentry *, struct vfsmount *,extern struct file *dentry_open (const struct path *,int ,const struct cred *);extern int filp_close (struct file *,fl_owner_t id); +extern struct filename *getname_flags (const char __user *,int ,int *);extern struct filename *getname (const char __user *);extern struct filename *getname_kernel (const char *);
include/linux/sched.h +4 -0
@@ -2430,6 +2430,10 @@ extern void do_group_exit(int);extern int do_execve (struct filename *,const char __user *const __user *,const char __user *const __user *); +extern int do_execveat (int ,struct filename *, +const char __user *const __user *, +const char __user *const __user *, +int );extern long do_fork (unsigned long ,unsigned long ,unsigned long ,int __user *,int __user *);struct task_struct *fork_idle (int );extern pid_t kernel_thread (int (*fn)(void *),void *arg,unsigned long flags);
include/linux/syscalls.h +4 -0
@@ -876,4 +876,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags); +asmlinkage long sys_execveat (int dfd,const char __user *filename, +const char __user *const __user *argv, +const char __user *const __user *envp,int flags); +#endif
include/uapi/asm-generic/unistd.h +3 -1
@@ -705,9 +705,11 @@ __SYSCALL(__NR_seccomp, sys_seccomp) __SYSCALL(__NR_getrandom, sys_getrandom)#define __NR_memfd_create 279 __SYSCALL(__NR_memfd_create, sys_memfd_create) +#define __NR_execveat 280 +__SC_COMP(__NR_execveat, sys_execveat, compat_sys_execveat)#undef __NR_syscalls -#define __NR_syscalls 280 +#define __NR_syscalls 281/* * All syscalls below here should go away really,
kernel/sys_ni.c +3 -0
@@ -218,3 +218,6 @@ cond_syscall(sys_kcmp);/* operate on Secure Computing state */ cond_syscall (sys_seccomp); + +/* execveat */ +cond_syscall (sys_execveat);
lib/audit.c +3 -0
@@ -54,6 +54,9 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_socketcall:return 4 ;#endif +#ifdef __NR_execveat +case __NR_execveat: +#endif case __NR_execve:return 5 ;default :
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH v5 2/3 syscalls,x86: add selftest for execveat(2)
In reply to David Drysdale--- tools/testing/selftests/Makefile | 1 + tools/testing/selftests/exec/.gitignore | 6 + tools/testing/selftests/exec/Makefile | 22 +++ tools/testing/selftests/exec/execveat.c | 295 ++++++++++++++++++++++++++++++++ 4 files changed, 324 insertions(+) create mode 100644 tools/testing/selftests/exec/.gitignore create mode 100644 tools/testing/selftests/exec/Makefile create mode 100644 tools/testing/selftests/exec/execveat.c
tools/testing/selftests/Makefile +1 -0
@@ -14,6 +14,7 @@ TARGETS += powerpc TARGETS += user TARGETS += sysctl TARGETS += firmware +TARGETS += exec TARGETS_HOTPLUG = cpu-hotplug TARGETS_HOTPLUG += memory-hotplug
tools/testing/selftests/exec/.gitignore new file +6 -0
@@ -0,0 +1,6 @@ +subdir* +script* +execveat +execveat.symlink +execveat.moved +execveat.ephemeral
tools/testing/selftests/exec/Makefile new file +22 -0
@@ -0,0 +1,22 @@ +CC = $(CROSS_COMPILE)gcc +CFLAGS = -Wall +BINARIES = execveat +DEPS = execveat.symlink script subdir +all: $(BINARIES) $(DEPS) + +subdir: + mkdir -p $@ +script: + echo '#!/bin/sh' > $@ + echo 'exit $$*' >> $@ + chmod +x $@ +execveat.symlink: execveat + ln -s -f $< $@ +%: %.c + $(CC) $(CFLAGS) -o $@ $^ + +run_tests: all + ./execveat + +clean: + rm -rf $(BINARIES) $(DEPS) subdir.moved execveat.moved
tools/testing/selftests/exec/execveat.c new file +295 -0
295 lines not on this page · raw message
Signature · 1 line
2.1.0.rc2.206.gedb03e5
PATCH v5 3/3 execveat.2: initial man page for execveat(2)
In reply to David Drysdale--- man2/execveat.2 | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 man2/execveat.2
man2/execveat.2 new file +144 -0
@@ -0,0 +1,144 @@ +.\" Copyright (c) 2014 Google, Inc. +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by +.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH EXECVEAT 2 2014-04-02 "Linux" "Linux Programmer's Manual" +.SH NAME +execveat \- execute program relative to a directory file descriptor +.SH SYNOPSIS +.B #include <unistd.h> +.sp +.BI "int execve(int " fd ", const char *" pathname "," +.br +.BI " char *const " argv "[], char *const " envp "[]," +.br +.BI " int " flags); +.SH DESCRIPTION +The +.BR execveat () +system call executes the program pointed to by the combination of \fIfd\fP and \fIpathname\fP. +The +.BR execveat () +system call operates in exactly the same way as +.BR execve (2), +except for the differences described in this manual page. + +If the pathname given in +.I pathname +is relative, then it is interpreted relative to the directory +referred to by the file descriptor +.I fd +(rather than relative to the current working directory of +the calling process, as is done by +.BR execve (2) +for a relative pathname). + +If +.I pathname +is relative and +.I fd +is the special value +.BR AT_FDCWD , +then +.I pathname +is interpreted relative to the current working +directory of the calling process (like +.BR execve (2)). + +If +.I pathname +is absolute, then +.I fd +is ignored. + +If +.I pathname +is an empty string and the +.BR AT_EMPTY_PATH +flag is specified, then the file descriptor +.I fd +specifies the file to be executed. + +.I flags +can either be 0, or include the following flags: +.TP +.BR AT_EMPTY_PATH +If +.I pathname +is an empty string, operate on the file referred to by +.IR fd +(which may have been obtained using the +.BR open (2) +.B O_PATH +flag). +.TP +.B AT_SYMLINK_NOFOLLOW +If the file identified by +.I fd +and a non-NULL +.I pathname +is a symbolic link, then the call fails with the error +.BR EINVAL . +.SH "RETURN VALUE" +On success, +.BR execveat () +does not return. On error \-1 is returned, and +.I errno +is set appropriately. +.SH ERRORS +The same errors that occur for +.BR execve (2) +can also occur for +.BR execveat (). +The following additional errors can occur for +.BR execveat (): +.TP +.B EBADF +.I fd +is not a valid file descriptor. +.TP +.B EINVAL +Invalid flag specified in +.IR flags . +.TP +.B ENOTDIR +.I pathname +is relative and +.I fd +is a file descriptor referring to a file other than a directory. +.SH VERSIONS +.BR execveat () +was added to Linux in kernel 3.???. +.SH NOTES +In addition to the reasons explained in +.BR openat (2), +the +.BR execveat () +system call is also needed to allow +.BR fexecve (3) +to be implemented on systems that do not have the +.I /proc +filesystem mounted. +.SH SEE ALSO +.BR execve (2), +.BR fexecve (3)
Signature · 1 line
2.1.0.rc2.206.gedb03e5
Re: [PATCHv5 man-pages 3/3] execveat.2: initial man page for execveat(2)
Quoted 33 lines · David Drysdale
Signed-off-by David Drysdale <[email protected]>--- man2/execveat.2 | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 man2/execveat.2man2/execveat.2 new file +21
-0@@ -0,0 +1,144 @@ +.\" Copyright (c) 2014 Google, Inc. +.\" +.\" %%%LICENSE_START(VERBATIM) +.\" Permission is granted to make and distribute verbatim copies of this +.\" manual provided the copyright notice and this permission notice are +.\" preserved on all copies. +.\" +.\" Permission is granted to copy and distribute modified versions of this +.\" manual under the conditions for verbatim copying, provided that the +.\" entire resulting derived work is distributed under the terms of a +.\" permission notice identical to this one. +.\" +.\" Since the Linux kernel and libraries are constantly changing, this +.\" manual page may be incorrect or out-of-date. The author(s) assume no +.\" responsibility for errors or omissions, or for damages resulting from +.\" the use of the information contained herein. The author(s) may not +.\" have taken the same level of care in the production of this manual, +.\" which is licensed free of charge, as they might when working +.\" professionally. +.\" +.\" Formatted or processed versions of this manual, if unaccompanied by
man2/execveat.2 line 31
Quoted 10 lines
man2/execveat.2 new file +10
-0+.\" the source, must acknowledge the copyright and authors of this work. +.\" %%%LICENSE_END +.\" +.TH EXECVEAT 2 2014-04-02 "Linux" "Linux Programmer's Manual" +.SH NAME +execveat \- execute program relative to a directory file descriptor +.SH SYNOPSIS +.B #include <unistd.h> +.sp +.BI "int execve(int " fd ", const char *" pathname ","
^^^^^^ That needs to be execveat
Quoted 113 lines
man2/execveat.2 new file +113
-0+.br +.BI " char *const " argv "[], char *const " envp "[]," +.br +.BI " int " flags); +.SH DESCRIPTION +The +.BR execveat () +system call executes the program pointed to by the combination of \fIfd\fP and \fIpathname\fP. +The +.BR execveat () +system call operates in exactly the same way as +.BR execve (2), +except for the differences described in this manual page. + +If the pathname given in +.I pathname +is relative, then it is interpreted relative to the directory +referred to by the file descriptor +.I fd +(rather than relative to the current working directory of +the calling process, as is done by +.BR execve (2) +for a relative pathname). + +If +.I pathname +is relative and +.I fd +is the special value +.BR AT_FDCWD , +then +.I pathname +is interpreted relative to the current working +directory of the calling process (like +.BR execve (2)). + +If +.I pathname +is absolute, then +.I fd +is ignored. + +If +.I pathname +is an empty string and the +.BR AT_EMPTY_PATH +flag is specified, then the file descriptor +.I fd +specifies the file to be executed. + +.I flags +can either be 0, or include the following flags: +.TP +.BR AT_EMPTY_PATH +If +.I pathname +is an empty string, operate on the file referred to by +.IR fd +(which may have been obtained using the +.BR open (2) +.B O_PATH +flag). +.TP +.B AT_SYMLINK_NOFOLLOW +If the file identified by +.I fd +and a non-NULL +.I pathname +is a symbolic link, then the call fails with the error +.BR EINVAL . +.SH "RETURN VALUE" +On success, +.BR execveat () +does not return. On error \-1 is returned, and +.I errno +is set appropriately. +.SH ERRORS +The same errors that occur for +.BR execve (2) +can also occur for +.BR execveat (). +The following additional errors can occur for +.BR execveat (): +.TP +.B EBADF +.I fd +is not a valid file descriptor. +.TP +.B EINVAL +Invalid flag specified in +.IR flags . +.TP +.B ENOTDIR +.I pathname +is relative and +.I fd +is a file descriptor referring to a file other than a directory. +.SH VERSIONS +.BR execveat () +was added to Linux in kernel 3.???. +.SH NOTES +In addition to the reasons explained in +.BR openat (2), +the +.BR execveat () +system call is also needed to allow +.BR fexecve (3) +to be implemented on systems that do not have the +.I /proc +filesystem mounted. +.SH SEE ALSO +.BR execve (2), +.BR fexecve (3)
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
In reply to David DrysdaleQuoted 181 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.In addition, if the filename is empty and
AT_EMPTY_PATHis specified,execveat()executes the file to which the file descriptor refers. This replicates the functionality offexecve(), which is a system call in other UNIXen, but in Linux glibc it depends on opening "/proc/self/fd/<fd>" (and so relies on /proc being mounted).The filename fed to the executed program as argv[0] (or the name of the script fed to a script interpreter) will be of the form "/dev/fd/<fd>" (for an empty filename) or "/dev/fd/<fd>/<filename>", effectively reflecting how the executable was found. This does however mean that execution of a script in a /proc-less environment won't work.
Only x86-64, i386 and x32 ABIs are supported in this patch.
Based on patches by Meredydd Luff <[email protected]>
Signed-off-by David Drysdale <[email protected]>--- arch/x86/ia32/audit.c | 1 + arch/x86/ia32/ia32entry.S | 1 + arch/x86/kernel/audit_64.c | 1 + arch/x86/kernel/entry_64.S | 28 ++++++++ arch/x86/syscalls/syscall_32.tbl | 1 + arch/x86/syscalls/syscall_64.tbl | 2 + arch/x86/um/sys_call_table_64.c | 1 + fs/exec.c | 130 ++++++++++++++++++++++++++++++++++---- fs/namei.c | 2 +- include/linux/compat.h | 3 + include/linux/fs.h | 1 + include/linux/sched.h | 4 ++ include/linux/syscalls.h | 4 ++ include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 3 + lib/audit.c | 3 + 16 files changed, 173 insertions(+), 16 deletions(-)arch/x86/ia32/audit.c +1
-0@@ -35,6 +35,7 @@ int ia32_classify_syscall(unsigned syscall)case __NR_socketcall:return 4 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 1 ;arch/x86/ia32/ia32entry.S +1
-0@@ -464,6 +464,7 @@ GLOBAL(\label) PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn PTREGSCALL stub32_sigreturn, sys32_sigreturn PTREGSCALL stub32_execve, compat_sys_execve + PTREGSCALL stub32_execveat, compat_sys_execveat PTREGSCALL stub32_fork, sys_fork PTREGSCALL stub32_vfork, sys_vforkarch/x86/kernel/audit_64.c +1
-0@@ -50,6 +50,7 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_openat:return 3 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 0 ;arch/x86/kernel/entry_64.S +28
-0@@ -665,6 +665,20 @@ ENTRY(stub_execve) CFI_ENDPROC END(stub_execve) +ENTRY(stub_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_execveat) + /* * sigreturn is special because it needs to restore all registers on return. * This cannot be done with SYSRET, so use the IRET return path instead.@@ -710,6 +724,20 @@ ENTRY(stub_x32_execve) CFI_ENDPROC END(stub_x32_execve) +ENTRY(stub_x32_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call compat_sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_x32_execveat) + #endif /*arch/x86/syscalls/syscall_32.tbl +1
-0@@ -363,3 +363,4 @@ 354 i386 seccomp sys_seccomp 355 i386 getrandom sys_getrandom 356 i386 memfd_create sys_memfd_create +357 i386 execveat sys_execveat stub32_execveatarch/x86/syscalls/syscall_64.tbl +2
-0@@ -327,6 +327,7 @@ 318 common getrandom sys_getrandom 319 common memfd_create sys_memfd_create 320 common kexec_file_load sys_kexec_file_load +321 64 execveat stub_execveat # # x32-specific system call numbers start at 512 to avoid cache impact@@ -365,3 +366,4 @@ 542 x32 getsockopt compat_sys_getsockopt 543 x32 io_setup compat_sys_io_setup 544 x32 io_submit compat_sys_io_submit +545 x32 execveat stub_x32_execveatarch/x86/um/sys_call_table_64.c +1
-0@@ -31,6 +31,7 @@#define stub_fork sys_fork#define stub_vfork sys_vfork#define stub_execve sys_execve +#define stub_execveat sys_execveat#define stub_rt_sigreturn sys_rt_sigreturn#define __SYSCALL_COMMON (nr, sym, compat) __SYSCALL_64(nr, sym, compat)fs/exec.c +5
-1@@ -747,7 +747,7 @@ EXPORT_SYMBOL(setup_arg_pages);#endif /* CONFIG_MMU */ -static struct file *do_open_exec (struct filename *name) +static struct file *do_open_execat (int fd,struct filename *name,int flags) {struct file *file;int err;@@ -757,10 +757,34 @@ static struct file *do_open_exec(struct filename *name) .intent = LOOKUP_OPEN, .lookup_flags = LOOKUP_FOLLOW, }; +static const struct open_flags open_exec_nofollow_flags = { + .open_flag =O_LARGEFILE |O_RDONLY | __FMODE_EXEC, + .acc_mode =MAY_EXEC |MAY_OPEN , + .intent =LOOKUP_OPEN ,
fs/exec.c static struct file *do_open_exec(struct filename *name) · line 770
Quoted 10 lines
fs/exec.c +6
-3+ .lookup_flags = 0, + }; - file =do_filp_open (AT_FDCWD , name, &open_exec_flags); -if (IS_ERR (file)) - goto out; +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +return ERR_PTR (-EINVAL ); + +if (name->name[0 ] !='\0' ) {
Is it really necessary to special case AT_EMPTY_PATH here. I would have thought the existing logic in namei.c would have been fine assuning we passed LOOKUP_EMPTY.
Quoted 300 lines
fs/exec.c +105
-10+const struct open_flags *oflags = ((flags &AT_SYMLINK_NOFOLLOW ) + ? &open_exec_nofollow_flags + : &open_exec_flags); + + file =do_filp_open (fd, name, oflags); +if (IS_ERR (file)) + goto out; + }else { + file =fget (fd); +if (!file) +return ERR_PTR (-EBADF ); + + err =inode_permission (file->f_path.dentry->d_inode, + open_exec_flags.acc_mode); +if (err) + goto exit; + } err = -EACCES ;if (!S_ISREG (file_inode (file)->i_mode))@@ -769,12 +793,13 @@ static struct file *do_open_exec(struct filename *name)if (file->f_path.mnt->mnt_flags &MNT_NOEXEC ) goto exit; -fsnotify_open (file); - err =deny_write_access (file);if (err) goto exit; +if (name->name[0 ] !='\0' ) +fsnotify_open (file); + out:return file;@@ -786,7 +811,7 @@ exit:struct file *open_exec (const char *name) {struct filename tmp = { .name = name }; -return do_open_exec (&tmp); +return do_open_execat (AT_FDCWD , &tmp,0 ); }EXPORT_SYMBOL (open_exec);@@ -1422,10 +1447,12 @@ static int exec_binprm(struct linux_binprm *bprm)/* * sys_execve() executes a new program. */ -static int do_execve_common (struct filename *filename, -struct user_arg_ptr argv, -struct user_arg_ptr envp) +static int do_execveat_common (int fd,struct filename *filename, +struct user_arg_ptr argv, +struct user_arg_ptr envp, +int flags) { +char *pathbuf =NULL ;struct linux_binprm *bprm;struct file *file;struct files_struct *displaced;@@ -1466,7 +1493,7 @@ static int do_execve_common(struct filename *filename,check_unsafe_exec (bprm); current->in_execve =1 ; - file =do_open_exec (filename); + file =do_open_execat (fd, filename, flags); retval =PTR_ERR (file);if (IS_ERR (file)) goto out_unmark;@@ -1474,7 +1501,27 @@ static int do_execve_common(struct filename *filename,sched_exec (); bprm->file = file; - bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or +* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd); +else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name); + } + bprm->interp = bprm->filename; retval =bprm_mm_init (bprm);if (retval)@@ -1532,6 +1579,7 @@ out_unmark: out_free:free_bprm (bprm); +kfree (pathbuf); out_files:if (displaced)@@ -1547,7 +1595,18 @@ int do_execve(struct filename *filename, {struct user_arg_ptr argv = { .ptr.native = __argv };struct user_arg_ptr envp = { .ptr.native = __envp }; -return do_execve_common (filename, argv, envp); +return do_execveat_common (AT_FDCWD , filename, argv, envp,0 ); +} + +int do_execveat (int fd,struct filename *filename, +const char __user *const __user *__argv, +const char __user *const __user *__envp, +int flags) +{ +struct user_arg_ptr argv = { .ptr.native = __argv }; +struct user_arg_ptr envp = { .ptr.native = __envp }; + +return do_execveat_common (fd, filename, argv, envp, flags); }#ifdef CONFIG_COMPAT @@ -1563,7 +1622,23 @@ static int compat_do_execve(struct filename *filename, .is_compat = true, .ptr.compat = __envp, }; -return do_execve_common (filename, argv, envp); +return do_execveat_common (AT_FDCWD , filename, argv, envp,0 ); +} + +static int compat_do_execveat (int fd,struct filename *filename, +const compat_uptr_t __user *__argv, +const compat_uptr_t __user *__envp, +int flags) +{ +struct user_arg_ptr argv = { + .is_compat = true, + .ptr.compat = __argv, + }; +struct user_arg_ptr envp = { + .is_compat = true, + .ptr.compat = __envp, + }; +return do_execveat_common (fd, filename, argv, envp, flags); }#endif @@ -1603,6 +1678,20 @@ SYSCALL_DEFINE3(execve, {return do_execve (getname (filename), argv, envp); } + +SYSCALL_DEFINE5 (execveat, + int, fd, const char __user *, filename, + const char __user *const __user *, argv, + const char __user *const __user *, envp, + int, flags) +{ +int lookup_flags = (flags &AT_EMPTY_PATH ) ?LOOKUP_EMPTY :0 ; + +return do_execveat (fd, +getname_flags (filename, lookup_flags,NULL ), + argv, envp, flags); +} +#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename, const compat_uptr_t __user *, argv,@@ -1610,4 +1699,17 @@ COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename, {return compat_do_execve (getname (filename), argv, envp); } + +COMPAT_SYSCALL_DEFINE5 (execveat, int, fd, + const char __user *, filename, + const compat_uptr_t __user *, argv, + const compat_uptr_t __user *, envp, + int, flags) +{ +int lookup_flags = (flags &AT_EMPTY_PATH ) ?LOOKUP_EMPTY :0 ; + +return compat_do_execveat (fd, +getname_flags (filename, lookup_flags,NULL ), + argv, envp, flags); +}#endif fs/namei.c +1
-1@@ -130,7 +130,7 @@ void final_putname(struct filename *name)#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) -static struct filename * +struct filename *getname_flags (const char __user *filename,int flags,int *empty) {struct filename *result, *err;include/linux/compat.h +3
-0@@ -357,6 +357,9 @@ asmlinkage long compat_sys_lseek(unsigned int, compat_off_t, unsigned int);asmlinkage long compat_sys_execve (const char __user *filename,const compat_uptr_t __user *argv,const compat_uptr_t __user *envp); +asmlinkage long compat_sys_execveat (int dfd,const char __user *filename, +const compat_uptr_t __user *argv, +const compat_uptr_t __user *envp,int flags); asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp, compat_ulong_t __user *exp,include/linux/fs.h +1
-0@@ -2060,6 +2060,7 @@ extern struct file *file_open_root(struct dentry *, struct vfsmount *,extern struct file *dentry_open (const struct path *,int ,const struct cred *);extern int filp_close (struct file *,fl_owner_t id); +extern struct filename *getname_flags (const char __user *,int ,int *);extern struct filename *getname (const char __user *);extern struct filename *getname_kernel (const char *);include/linux/sched.h +4
-0@@ -2430,6 +2430,10 @@ extern void do_group_exit(int);extern int do_execve (struct filename *,const char __user *const __user *,const char __user *const __user *); +extern int do_execveat (int ,struct filename *, +const char __user *const __user *, +const char __user *const __user *, +int );extern long do_fork (unsigned long ,unsigned long ,unsigned long ,int __user *,int __user *);struct task_struct *fork_idle (int );extern pid_t kernel_thread (int (*fn)(void *),void *arg,unsigned long flags);include/linux/syscalls.h +4
-0@@ -876,4 +876,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags); +asmlinkage long sys_execveat (int dfd,const char __user *filename, +const char __user *const __user *argv, +const char __user *const __user *envp,int flags); +#endif include/uapi/asm-generic/unistd.h +3
-1@@ -705,9 +705,11 @@ __SYSCALL(__NR_seccomp, sys_seccomp) __SYSCALL(__NR_getrandom, sys_getrandom)#define __NR_memfd_create 279 __SYSCALL(__NR_memfd_create, sys_memfd_create) +#define __NR_execveat 280 +__SC_COMP(__NR_execveat, sys_execveat, compat_sys_execveat)#undef __NR_syscalls -#define __NR_syscalls 280 +#define __NR_syscalls 281/* * All syscalls below here should go away really, kernel/sys_ni.c +3
-0@@ -218,3 +218,6 @@ cond_syscall(sys_kcmp);/* operate on Secure Computing state */ cond_syscall (sys_seccomp); + +/* execveat */ +cond_syscall (sys_execveat);lib/audit.c +3
-0@@ -54,6 +54,9 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_socketcall:return 4 ;#endif +#ifdef __NR_execveat +case __NR_execveat: +#endif case __NR_execve:return 5 ;default :
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
In reply to David DrysdaleQuoted 13 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.bprm->file = file;fs/exec.c +6
-1- bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or
fs/exec.c static int do_execve_common(struct filename *filename, · line 1519
Quoted 10 lines
fs/exec.c +10
-0+* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd);
If the fd is O_CLOEXEC, then this will result in a confused child process. Should we fail exec attempts like that for non-static programs? (E.g. set filename to "" or something and fix up the binfmt drivers to handle that?)
fs/exec.c static int do_execve_common(struct filename *filename, · line 1522
Quoted 3 lines
fs/exec.c +3
-0+else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name);
Does this need to handle the case where the result exceeds PATH_MAX?
--Andy
Re: [PATCHv5 man-pages 3/3] execveat.2: initial man page for execveat(2)
In reply to Eric W. BiedermanQuoted 9 lines · Eric W. Biederman
Quoted 7 lines · David Drysdale
+.TH EXECVEAT 2 2014-04-02 "Linux" "Linux Programmer's Manual" +.SH NAME
+execveat \- execute program relative to a directory file descriptor +.SH SYNOPSIS
+.B #include <unistd.h>
+.sp
+.BI "int execve(int " fd ", const char *" pathname ","^^^^^^ That needs to be execveat
Ah yes, so it does -- thanks for spotting.
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
In reply to Eric W. BiedermanQuoted 186 lines · Eric W. Biederman
Quoted 184 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.In addition, if the filename is empty and
AT_EMPTY_PATHis specified,execveat()executes the file to which the file descriptor refers. This replicates the functionality offexecve(), which is a system call in other UNIXen, but in Linux glibc it depends on opening "/proc/self/fd/<fd>" (and so relies on /proc being mounted).The filename fed to the executed program as argv[0] (or the name of the script fed to a script interpreter) will be of the form "/dev/fd/<fd>" (for an empty filename) or "/dev/fd/<fd>/<filename>", effectively reflecting how the executable was found. This does however mean that execution of a script in a /proc-less environment won't work.
Only x86-64, i386 and x32 ABIs are supported in this patch.
Based on patches by Meredydd Luff <[email protected]>
Signed-off-by David Drysdale <[email protected]>--- arch/x86/ia32/audit.c | 1 + arch/x86/ia32/ia32entry.S | 1 + arch/x86/kernel/audit_64.c | 1 + arch/x86/kernel/entry_64.S | 28 ++++++++ arch/x86/syscalls/syscall_32.tbl | 1 + arch/x86/syscalls/syscall_64.tbl | 2 + arch/x86/um/sys_call_table_64.c | 1 + fs/exec.c | 130 ++++++++++++++++++++++++++++++++++---- fs/namei.c | 2 +- include/linux/compat.h | 3 + include/linux/fs.h | 1 + include/linux/sched.h | 4 ++ include/linux/syscalls.h | 4 ++ include/uapi/asm-generic/unistd.h | 4 +- kernel/sys_ni.c | 3 + lib/audit.c | 3 + 16 files changed, 173 insertions(+), 16 deletions(-)arch/x86/ia32/audit.c +1
-0@@ -35,6 +35,7 @@ int ia32_classify_syscall(unsigned syscall)case __NR_socketcall:return 4 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 1 ;arch/x86/ia32/ia32entry.S +1
-0@@ -464,6 +464,7 @@ GLOBAL(\label) PTREGSCALL stub32_rt_sigreturn, sys32_rt_sigreturn PTREGSCALL stub32_sigreturn, sys32_sigreturn PTREGSCALL stub32_execve, compat_sys_execve + PTREGSCALL stub32_execveat, compat_sys_execveat PTREGSCALL stub32_fork, sys_fork PTREGSCALL stub32_vfork, sys_vforkarch/x86/kernel/audit_64.c +1
-0@@ -50,6 +50,7 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_openat:return 3 ;case __NR_execve: +case __NR_execveat:return 5 ;default :return 0 ;arch/x86/kernel/entry_64.S +28
-0@@ -665,6 +665,20 @@ ENTRY(stub_execve) CFI_ENDPROC END(stub_execve) +ENTRY(stub_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_execveat) + /* * sigreturn is special because it needs to restore all registers on return. * This cannot be done with SYSRET, so use the IRET return path instead.@@ -710,6 +724,20 @@ ENTRY(stub_x32_execve) CFI_ENDPROC END(stub_x32_execve) +ENTRY(stub_x32_execveat) + CFI_STARTPROC + addq $8, %rsp + PARTIAL_FRAME 0 + SAVE_REST + FIXUP_TOP_OF_STACK %r11 + call compat_sys_execveat + RESTORE_TOP_OF_STACK %r11 + movq %rax,RAX(%rsp) + RESTORE_REST + jmp int_ret_from_sys_call + CFI_ENDPROC +END(stub_x32_execveat) + #endif /*arch/x86/syscalls/syscall_32.tbl +1
-0@@ -363,3 +363,4 @@ 354 i386 seccomp sys_seccomp 355 i386 getrandom sys_getrandom 356 i386 memfd_create sys_memfd_create +357 i386 execveat sys_execveat stub32_execveatarch/x86/syscalls/syscall_64.tbl +2
-0@@ -327,6 +327,7 @@ 318 common getrandom sys_getrandom 319 common memfd_create sys_memfd_create 320 common kexec_file_load sys_kexec_file_load +321 64 execveat stub_execveat # # x32-specific system call numbers start at 512 to avoid cache impact@@ -365,3 +366,4 @@ 542 x32 getsockopt compat_sys_getsockopt 543 x32 io_setup compat_sys_io_setup 544 x32 io_submit compat_sys_io_submit +545 x32 execveat stub_x32_execveatarch/x86/um/sys_call_table_64.c +1
-0@@ -31,6 +31,7 @@#define stub_fork sys_fork#define stub_vfork sys_vfork#define stub_execve sys_execve +#define stub_execveat sys_execveat#define stub_rt_sigreturn sys_rt_sigreturn#define __SYSCALL_COMMON (nr, sym, compat) __SYSCALL_64(nr, sym, compat)fs/exec.c +7
-1@@ -747,7 +747,7 @@ EXPORT_SYMBOL(setup_arg_pages);#endif /* CONFIG_MMU */ -static struct file *do_open_exec (struct filename *name) +static struct file *do_open_execat (int fd,struct filename *name,int flags) {struct file *file;int err;@@ -757,10 +757,34 @@ static struct file *do_open_exec(struct filename *name) .intent = LOOKUP_OPEN, .lookup_flags = LOOKUP_FOLLOW, }; +static const struct open_flags open_exec_nofollow_flags = { + .open_flag =O_LARGEFILE |O_RDONLY | __FMODE_EXEC, + .acc_mode =MAY_EXEC |MAY_OPEN , + .intent =LOOKUP_OPEN , + .lookup_flags =0 , + };
Quoted 11 lines
fs/exec.c static struct file *do_open_exec(struct filename *name) · line 770
Quoted 7 lines
fs/exec.c +4
-3- file =do_filp_open (AT_FDCWD , name, &open_exec_flags); -if (IS_ERR (file)) - goto out; +if ((flags & ~(AT_SYMLINK_NOFOLLOW |AT_EMPTY_PATH )) !=0 ) +return ERR_PTR (-EINVAL ); + +if (name->name[0 ] !='\0' ) {Is it really necessary to special case
AT_EMPTY_PATHhere. I would have thought the existing logic innamei.cwould have been fine assuning we passedLOOKUP_EMPTY.
Just using do_filp_open() throughout looks mostly plausible on a quick experiment, but my initial version appears to make O_PATH fds unexpectedly fexecve()-able (I'm glad I had a test case for that).
I'll look for a way around that, hopefully without an explicit special case.
Quoted 300 lines
Quoted 300 lines
fs/exec.c +105
-10+const struct open_flags *oflags = ((flags &AT_SYMLINK_NOFOLLOW ) + ? &open_exec_nofollow_flags + : &open_exec_flags); + + file =do_filp_open (fd, name, oflags); +if (IS_ERR (file)) + goto out; + }else { + file =fget (fd); +if (!file) +return ERR_PTR (-EBADF ); + + err =inode_permission (file->f_path.dentry->d_inode, + open_exec_flags.acc_mode); +if (err) + goto exit; + } err = -EACCES ;if (!S_ISREG (file_inode (file)->i_mode))@@ -769,12 +793,13 @@ static struct file *do_open_exec(struct filename *name)if (file->f_path.mnt->mnt_flags &MNT_NOEXEC ) goto exit; -fsnotify_open (file); - err =deny_write_access (file);if (err) goto exit; +if (name->name[0 ] !='\0' ) +fsnotify_open (file); + out:return file;@@ -786,7 +811,7 @@ exit:struct file *open_exec (const char *name) {struct filename tmp = { .name = name }; -return do_open_exec (&tmp); +return do_open_execat (AT_FDCWD , &tmp,0 ); }EXPORT_SYMBOL (open_exec);@@ -1422,10 +1447,12 @@ static int exec_binprm(struct linux_binprm *bprm)/* * sys_execve() executes a new program. */ -static int do_execve_common (struct filename *filename, -struct user_arg_ptr argv, -struct user_arg_ptr envp) +static int do_execveat_common (int fd,struct filename *filename, +struct user_arg_ptr argv, +struct user_arg_ptr envp, +int flags) { +char *pathbuf =NULL ;struct linux_binprm *bprm;struct file *file;struct files_struct *displaced;@@ -1466,7 +1493,7 @@ static int do_execve_common(struct filename *filename,check_unsafe_exec (bprm); current->in_execve =1 ; - file =do_open_exec (filename); + file =do_open_execat (fd, filename, flags); retval =PTR_ERR (file);if (IS_ERR (file)) goto out_unmark;@@ -1474,7 +1501,27 @@ static int do_execve_common(struct filename *filename,sched_exec (); bprm->file = file; - bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or +* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd); +else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name); + } + bprm->interp = bprm->filename; retval =bprm_mm_init (bprm);if (retval)@@ -1532,6 +1579,7 @@ out_unmark: out_free:free_bprm (bprm); +kfree (pathbuf); out_files:if (displaced)@@ -1547,7 +1595,18 @@ int do_execve(struct filename *filename, {struct user_arg_ptr argv = { .ptr.native = __argv };struct user_arg_ptr envp = { .ptr.native = __envp }; -return do_execve_common (filename, argv, envp); +return do_execveat_common (AT_FDCWD , filename, argv, envp,0 ); +} + +int do_execveat (int fd,struct filename *filename, +const char __user *const __user *__argv, +const char __user *const __user *__envp, +int flags) +{ +struct user_arg_ptr argv = { .ptr.native = __argv }; +struct user_arg_ptr envp = { .ptr.native = __envp }; + +return do_execveat_common (fd, filename, argv, envp, flags); }#ifdef CONFIG_COMPAT @@ -1563,7 +1622,23 @@ static int compat_do_execve(struct filename *filename, .is_compat = true, .ptr.compat = __envp, }; -return do_execve_common (filename, argv, envp); +return do_execveat_common (AT_FDCWD , filename, argv, envp,0 ); +} + +static int compat_do_execveat (int fd,struct filename *filename, +const compat_uptr_t __user *__argv, +const compat_uptr_t __user *__envp, +int flags) +{ +struct user_arg_ptr argv = { + .is_compat = true, + .ptr.compat = __argv, + }; +struct user_arg_ptr envp = { + .is_compat = true, + .ptr.compat = __envp, + }; +return do_execveat_common (fd, filename, argv, envp, flags); }#endif @@ -1603,6 +1678,20 @@ SYSCALL_DEFINE3(execve, {return do_execve (getname (filename), argv, envp); } + +SYSCALL_DEFINE5 (execveat, + int, fd, const char __user *, filename, + const char __user *const __user *, argv, + const char __user *const __user *, envp, + int, flags) +{ +int lookup_flags = (flags &AT_EMPTY_PATH ) ?LOOKUP_EMPTY :0 ; + +return do_execveat (fd, +getname_flags (filename, lookup_flags,NULL ), + argv, envp, flags); +} +#ifdef CONFIG_COMPAT COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename, const compat_uptr_t __user *, argv,@@ -1610,4 +1699,17 @@ COMPAT_SYSCALL_DEFINE3(execve, const char __user *, filename, {return compat_do_execve (getname (filename), argv, envp); } + +COMPAT_SYSCALL_DEFINE5 (execveat, int, fd, + const char __user *, filename, + const compat_uptr_t __user *, argv, + const compat_uptr_t __user *, envp, + int, flags) +{ +int lookup_flags = (flags &AT_EMPTY_PATH ) ?LOOKUP_EMPTY :0 ; + +return compat_do_execveat (fd, +getname_flags (filename, lookup_flags,NULL ), + argv, envp, flags); +}#endif fs/namei.c +1
-1@@ -130,7 +130,7 @@ void final_putname(struct filename *name)#define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) -static struct filename * +struct filename *getname_flags (const char __user *filename,int flags,int *empty) {struct filename *result, *err;include/linux/compat.h +3
-0@@ -357,6 +357,9 @@ asmlinkage long compat_sys_lseek(unsigned int, compat_off_t, unsigned int);asmlinkage long compat_sys_execve (const char __user *filename,const compat_uptr_t __user *argv,const compat_uptr_t __user *envp); +asmlinkage long compat_sys_execveat (int dfd,const char __user *filename, +const compat_uptr_t __user *argv, +const compat_uptr_t __user *envp,int flags); asmlinkage long compat_sys_select(int n, compat_ulong_t __user *inp, compat_ulong_t __user *outp, compat_ulong_t __user *exp,include/linux/fs.h +1
-0@@ -2060,6 +2060,7 @@ extern struct file *file_open_root(struct dentry *, struct vfsmount *,extern struct file *dentry_open (const struct path *,int ,const struct cred *);extern int filp_close (struct file *,fl_owner_t id); +extern struct filename *getname_flags (const char __user *,int ,int *);extern struct filename *getname (const char __user *);extern struct filename *getname_kernel (const char *);include/linux/sched.h +4
-0@@ -2430,6 +2430,10 @@ extern void do_group_exit(int);extern int do_execve (struct filename *,const char __user *const __user *,const char __user *const __user *); +extern int do_execveat (int ,struct filename *, +const char __user *const __user *, +const char __user *const __user *, +int );extern long do_fork (unsigned long ,unsigned long ,unsigned long ,int __user *,int __user *);struct task_struct *fork_idle (int );extern pid_t kernel_thread (int (*fn)(void *),void *arg,unsigned long flags);include/linux/syscalls.h +4
-0@@ -876,4 +876,8 @@ asmlinkage long sys_seccomp(unsigned int op, unsigned int flags,asmlinkage long sys_getrandom (char __user *buf,size_t count,unsigned int flags); +asmlinkage long sys_execveat (int dfd,const char __user *filename, +const char __user *const __user *argv, +const char __user *const __user *envp,int flags); +#endif include/uapi/asm-generic/unistd.h +3
-1@@ -705,9 +705,11 @@ __SYSCALL(__NR_seccomp, sys_seccomp) __SYSCALL(__NR_getrandom, sys_getrandom)#define __NR_memfd_create 279 __SYSCALL(__NR_memfd_create, sys_memfd_create) +#define __NR_execveat 280 +__SC_COMP(__NR_execveat, sys_execveat, compat_sys_execveat)#undef __NR_syscalls -#define __NR_syscalls 280 +#define __NR_syscalls 281/* * All syscalls below here should go away really, kernel/sys_ni.c +3
-0@@ -218,3 +218,6 @@ cond_syscall(sys_kcmp);/* operate on Secure Computing state */ cond_syscall (sys_seccomp); + +/* execveat */ +cond_syscall (sys_execveat);lib/audit.c +3
-0@@ -54,6 +54,9 @@ int audit_classify_syscall(int abi, unsigned syscall)case __NR_socketcall:return 4 ;#endif +#ifdef __NR_execveat +case __NR_execveat: +#endif case __NR_execve:return 5 ;default :
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
In reply to Andy LutomirskiQuoted 25 lines · Andy Lutomirski
Quoted 13 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.bprm->file = file;fs/exec.c +6
-1- bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or fs/exec.c static int do_execve_common(struct filename *filename, · line 1519
Quoted 10 lines
fs/exec.c +10
-0+* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd);
Quoted 4 lines
If the fd is
O_CLOEXEC, then this will result in a confused child process. Should we fail exec attempts like that for non-static programs? (E.g. set filename to "" or something and fix up the binfmt drivers to handle that?)
Isn't it just scripts that get confused here (as normal executables don't get to see brpm->filename)?
Given that we don't know which we have at this point, I'd suggest carrying on regardless. Or we could fall back to use the previous best-effort d_path() code for O_CLOEXEC fds. Thoughts?
Quoted 5 lines
fs/exec.c static int do_execve_common(struct filename *filename, · line 1522
Quoted 3 lines
fs/exec.c +3
-0+else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name);Does this need to handle the case where the result exceeds
PATH_MAX?
I guess we could kmalloc(strlen(filename->name) + 19) to avoid the possibility of failure, but that just defers the inevitable -- the interpreter won't be able to open the script file anyway. But it would at least then generate the appropriate error (ENAMETOOLONG rather than ENOENT).
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
Quoted 31 lines · David Drysdale
Quoted 25 lines · Andy Lutomirski
Quoted 13 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.bprm->file = file;fs/exec.c +6
-1- bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or fs/exec.c static int do_execve_common(struct filename *filename, · line 1519
Quoted 10 lines
fs/exec.c +10
-0+* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd);Quoted 4 lines
If the fd is
O_CLOEXEC, then this will result in a confused child process. Should we fail exec attempts like that for non-static programs? (E.g. set filename to "" or something and fix up the binfmt drivers to handle that?)
Quoted 6 lines
Isn't it just scripts that get confused here (as normal executables don't get to see
brpm->filename)?Given that we don't know which we have at this point, I'd suggest carrying on regardless. Or we could fall back to use the previous best-effort
d_path()code forO_CLOEXECfds. Thoughts?
How hard would it be to mark the bprm as not having a path for the binary? Then we could fail later on if and when we actually need the path.
I don't really have a strong opinion here, though. I do prefer actually failing the execveat call over succeeding but invoking a script interpreter than can't possibly work.
Quoted 11 lines
Quoted 6 lines
fs/exec.c static int do_execve_common(struct filename *filename, · line 1522
Quoted 4 lines
fs/exec.c +3
-0+else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name);Does this need to handle the case where the result exceeds
PATH_MAX?I guess we could
kmalloc(strlen(filename->name) + 19)to avoid the possibility of failure, but that just defers the inevitable -- the interpreter won't be able to open the script file anyway. But it would at least then generate the appropriate error (ENAMETOOLONG rather than ENOENT).
Depends whether anyone cares about bprm->filename. But I think the code should either return an error or allocate enough space.
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
[Oops, re-send remembering to turn on plaintext mode -- sorry]
Quoted 35 lines · Andy Lutomirski
Quoted 34 lines · David Drysdale
Quoted 25 lines · Andy Lutomirski
Quoted 13 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.bprm->file = file;fs/exec.c +6
-1- bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or fs/exec.c static int do_execve_common(struct filename *filename, · line 1519
Quoted 10 lines
fs/exec.c +10
-0+* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd);Quoted 4 lines
If the fd is
O_CLOEXEC, then this will result in a confused child process. Should we fail exec attempts like that for non-static programs? (E.g. set filename to "" or something and fix up the binfmt drivers to handle that?)Isn't it just scripts that get confused here (as normal executables don't get to see
brpm->filename)?
Quoted 7 lines
Quoted 3 lines
Given that we don't know which we have at this point, I'd suggest carrying on regardless. Or we could fall back to use the previous best-effort
d_path()code forO_CLOEXECfds. Thoughts?How hard would it be to mark the bprm as not having a path for the binary? Then we could fail later on if and when we actually need the path.
Adding a flag to bprm->interp_flags to indicate that the bprm->filename will be inaccessible after exec is straightforward. But I'm not sure who should/could make use of the flag...
Quoted 3 lines
I don't really have a strong opinion here, though. I do prefer actually failing the execveat call over succeeding but invoking a script interpreter than can't possibly work.
Yeah, but that involves the kernel code (e.g. fs/binfmt_script.c) making an assumption about what the interpreter is going to do -- specifically that it's going to try to open its argv[1]. Admittedly, that's a very likely assumption, but I'm not sure it's one the kernel should make -- a script like "#!/bin/echo" wouldn't be very useful, but fexecve()ing it would still work OK on a name like "/dev/fd/7" after fd 7 is closed.
(Also, we need some kind of non-empty name in bprm->filename, even if it's going to be inaccessible later, so that any LSM processing off of the bprm_set_creds()/bprm_check_security() hooks has something to work with; those hooks are pre-exec so the "/dev/fd/<fd>" part should still be OK at that point.)
So I guess I lean towards keeping "/dev/fd/<fd>/<path>" regardless.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
fs/exec.c static int do_execve_common(struct filename *filename, · line 1522
Quoted 4 lines
fs/exec.c +3
-0+else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name);Does this need to handle the case where the result exceeds
PATH_MAX?
Quoted 7 lines
Quoted 4 lines
I guess we could
kmalloc(strlen(filename->name) + 19)to avoid the possibility of failure, but that just defers the inevitable -- the interpreter won't be able to open the script file anyway. But it would at least then generate the appropriate error (ENAMETOOLONG rather than ENOENT).Depends whether anyone cares about
bprm->filename. But I think the code should either return an error or allocate enough space.
I'll allocate enough space.
Quoted 4 lines
--
Andy Lutomirski
AMA Capital Management, LLC
Re: [PATCHv5 1/3] syscalls,x86: implement execveat() system call
Quoted 54 lines · David Drysdale
[Oops, re-send remembering to turn on plaintext mode -- sorry]
Quoted 35 lines · Andy Lutomirski
Quoted 34 lines · David Drysdale
Quoted 25 lines · Andy Lutomirski
Quoted 13 lines · David Drysdale
Add a new system execveat(2) syscall.
execveat()is toexecve()asopenat()is toopen(): it takes a file descriptor that refers to a directory, and resolves the filename relative to that.bprm->file = file;fs/exec.c +6
-1- bprm->filename = bprm->interp = filename->name; +if (fd ==AT_FDCWD || filename->name[0 ] =='/' ) { + bprm->filename = filename->name; + }else { +/* +* Build a pathname that reflects how we got to the file, +* either "/dev/fd/<fd>" (for an empty filename) or fs/exec.c static int do_execve_common(struct filename *filename, · line 1519
Quoted 10 lines
fs/exec.c +10
-0+* "/dev/fd/<fd>/<filename>". +*/ + pathbuf =kmalloc (PATH_MAX ,GFP_TEMPORARY ); +if (!pathbuf) { + retval = -ENOMEM ; + goto out_unmark; + } + bprm->filename = pathbuf; +if (filename->name[0 ] =='\0' ) +sprintf (pathbuf,"/dev/fd/%d" , fd);Quoted 4 lines
If the fd is
O_CLOEXEC, then this will result in a confused child process. Should we fail exec attempts like that for non-static programs? (E.g. set filename to "" or something and fix up the binfmt drivers to handle that?)Isn't it just scripts that get confused here (as normal executables don't get to see
brpm->filename)?Quoted 7 lines
Quoted 3 lines
Given that we don't know which we have at this point, I'd suggest carrying on regardless. Or we could fall back to use the previous best-effort
d_path()code forO_CLOEXECfds. Thoughts?How hard would it be to mark the bprm as not having a path for the binary? Then we could fail later on if and when we actually need the path.
Adding a flag to
bprm->interp_flagsto indicate that thebprm->filenamewill be inaccessible after exec is straightforward. But I'm not sure who should/could make use of the flag...Quoted 3 lines
I don't really have a strong opinion here, though. I do prefer actually failing the execveat call over succeeding but invoking a script interpreter than can't possibly work.
Quoted 6 lines
Yeah, but that involves the kernel code (e.g.
fs/binfmt_script.c) making an assumption about what the interpreter is going to do -- specifically that it's going to try to open its argv[1]. Admittedly, that's a very likely assumption, but I'm not sure it's one the kernel should make -- a script like "#!/bin/echo" wouldn't be very useful, but fexecve()ing it would still work OK on a name like "/dev/fd/7" after fd 7 is closed.
Hmm. I'm unconvinced. If an important part of executing the script is passing it an argv[0] that can be opened, then I think we shouldn't allow a known-bad argv[0].
Quoted 30 lines
(Also, we need some kind of non-empty name in
bprm->filename, even if it's going to be inaccessible later, so that any LSM processing off of the bprm_set_creds()/bprm_check_security() hooks has something to work with; those hooks are pre-exec so the "/dev/fd/<fd>" part should still be OK at that point.)So I guess I lean towards keeping "/dev/fd/<fd>/<path>" regardless.
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
fs/exec.c static int do_execve_common(struct filename *filename, · line 1522
Quoted 4 lines
fs/exec.c +3
-0+else +snprintf (pathbuf,PATH_MAX , +"/dev/fd/%d/%s" , fd, filename->name);Does this need to handle the case where the result exceeds
PATH_MAX?Quoted 7 lines
Quoted 4 lines
I guess we could
kmalloc(strlen(filename->name) + 19)to avoid the possibility of failure, but that just defers the inevitable -- the interpreter won't be able to open the script file anyway. But it would at least then generate the appropriate error (ENAMETOOLONG rather than ENOENT).Depends whether anyone cares about
bprm->filename. But I think the code should either return an error or allocate enough space.I'll allocate enough space.
Quoted 4 lines
--
Andy Lutomirski
AMA Capital Management, LLC
Signature · 2 lines
Andy Lutomirski AMA Capital Management, LLC