From: Oleg Nesterov <oleg@redhat.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>,
LKML <linux-kernel@vger.kernel.org>,
linux-mm <linux-mm@kvack.org>,
pageexec@freemail.hu, Solar Designer <solar@openwall.com>,
Eugene Teo <eteo@redhat.com>,
Brad Spengler <spender@grsecurity.net>,
Roland McGrath <roland@redhat.com>,
Milton Miller <miltonm@bga.com>,
Linus Torvalds <torvalds@linux-foundation.org>
Subject: [PATCH v4 3/4] exec: unify do_execve/compat_do_execve code
Date: Sat, 5 Mar 2011 21:31:40 +0100 [thread overview]
Message-ID: <20110305203140.GD7546@redhat.com> (raw)
In-Reply-To: <20110305203040.GA7546@redhat.com>
Add the appropriate members into struct user_arg_ptr and teach
get_user_arg_ptr() to handle is_compat = T case correctly.
This allows us to remove the compat_do_execve() code from fs/compat.c
and reimplement compat_do_execve() as the trivial wrapper on top of
do_execve_common(is_compat => true).
In fact, this fixes another (minor) bug. "compat_uptr_t str" can
overflow after "str += len" in compat_copy_strings() if a 64bit
application execs via sys32_execve().
Unexport acct_arg_size() and get_arg_page(), fs/compat.c doesn't
need them any longer.
Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Reviewed-by: KOSAKI Motohiro <kosaki.motohiro@jp.fujitsu.com>
---
include/linux/binfmts.h | 4
fs/exec.c | 58 +++++++++--
fs/compat.c | 235 ------------------------------------------------
3 files changed, 46 insertions(+), 251 deletions(-)
--- 38/include/linux/binfmts.h~3_handle_compat_case 2011-03-05 21:13:45.000000000 +0100
+++ 38/include/linux/binfmts.h 2011-03-05 21:23:15.000000000 +0100
@@ -60,10 +60,6 @@ struct linux_binprm {
unsigned long loader, exec;
};
-extern void acct_arg_size(struct linux_binprm *bprm, unsigned long pages);
-extern struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
- int write);
-
#define BINPRM_FLAGS_ENFORCE_NONDUMP_BIT 0
#define BINPRM_FLAGS_ENFORCE_NONDUMP (1 << BINPRM_FLAGS_ENFORCE_NONDUMP_BIT)
--- 38/fs/exec.c~3_handle_compat_case 2011-03-05 21:22:42.000000000 +0100
+++ 38/fs/exec.c 2011-03-05 21:23:15.000000000 +0100
@@ -55,6 +55,7 @@
#include <linux/fs_struct.h>
#include <linux/pipe_fs_i.h>
#include <linux/oom.h>
+#include <linux/compat.h>
#include <asm/uaccess.h>
#include <asm/mmu_context.h>
@@ -164,7 +165,7 @@ out:
#ifdef CONFIG_MMU
-void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
+static void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
{
struct mm_struct *mm = current->mm;
long diff = (long)(pages - bprm->vma_pages);
@@ -183,7 +184,7 @@ void acct_arg_size(struct linux_binprm *
#endif
}
-struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
+static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
int write)
{
struct page *page;
@@ -302,11 +303,11 @@ static bool valid_arg_len(struct linux_b
#else
-void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
+static inline void acct_arg_size(struct linux_binprm *bprm, unsigned long pages)
{
}
-struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
+static struct page *get_arg_page(struct linux_binprm *bprm, unsigned long pos,
int write)
{
struct page *page;
@@ -396,17 +397,34 @@ err:
}
struct user_arg_ptr {
- const char __user *const __user *native;
+#ifdef CONFIG_COMPAT
+ bool is_compat;
+#endif
+ union {
+ const char __user *const __user *native;
+ compat_uptr_t __user *compat;
+ } ptr;
};
static const char __user *get_user_arg_ptr(struct user_arg_ptr argv, int nr)
{
- const char __user *ptr;
+ const char __user *native;
- if (get_user(ptr, argv.native + nr))
+#ifdef CONFIG_COMPAT
+ if (unlikely(argv.is_compat)) {
+ compat_uptr_t compat;
+
+ if (get_user(compat, argv.ptr.compat + nr))
+ return ERR_PTR(-EFAULT);
+
+ return compat_ptr(compat);
+ }
+#endif
+
+ if (get_user(native, argv.ptr.native + nr))
return ERR_PTR(-EFAULT);
- return ptr;
+ return native;
}
/*
@@ -416,7 +434,7 @@ static int count(struct user_arg_ptr arg
{
int i = 0;
- if (argv.native != NULL) {
+ if (argv.ptr.native != NULL) {
for (;;) {
const char __user *p = get_user_arg_ptr(argv, i);
@@ -539,7 +557,7 @@ int copy_strings_kernel(int argc, const
int r;
mm_segment_t oldfs = get_fs();
struct user_arg_ptr argv = {
- .native = (const char __user *const __user *)__argv,
+ .ptr.native = (const char __user *const __user *)__argv,
};
set_fs(KERNEL_DS);
@@ -1510,10 +1528,28 @@ int do_execve(const char *filename,
const char __user *const __user *__envp,
struct pt_regs *regs)
{
- struct user_arg_ptr argv = { .native = __argv };
- struct user_arg_ptr envp = { .native = __envp };
+ struct user_arg_ptr argv = { .ptr.native = __argv };
+ struct user_arg_ptr envp = { .ptr.native = __envp };
+ return do_execve_common(filename, argv, envp, regs);
+}
+
+#ifdef CONFIG_COMPAT
+int compat_do_execve(char *filename,
+ compat_uptr_t __user *__argv,
+ compat_uptr_t __user *__envp,
+ struct pt_regs *regs)
+{
+ struct user_arg_ptr argv = {
+ .is_compat = true,
+ .ptr.compat = __argv,
+ };
+ struct user_arg_ptr envp = {
+ .is_compat = true,
+ .ptr.compat = __envp,
+ };
return do_execve_common(filename, argv, envp, regs);
}
+#endif
void set_binfmt(struct linux_binfmt *new)
{
--- 38/fs/compat.c~3_handle_compat_case 2011-03-05 21:13:45.000000000 +0100
+++ 38/fs/compat.c 2011-03-05 21:23:15.000000000 +0100
@@ -1330,241 +1330,6 @@ compat_sys_openat(unsigned int dfd, cons
return do_sys_open(dfd, filename, flags, mode);
}
-/*
- * compat_count() counts the number of arguments/envelopes. It is basically
- * a copy of count() from fs/exec.c, except that it works with 32 bit argv
- * and envp pointers.
- */
-static int compat_count(compat_uptr_t __user *argv, int max)
-{
- int i = 0;
-
- if (argv != NULL) {
- for (;;) {
- compat_uptr_t p;
-
- if (get_user(p, argv))
- return -EFAULT;
- if (!p)
- break;
- argv++;
- if (i++ >= max)
- return -E2BIG;
-
- if (fatal_signal_pending(current))
- return -ERESTARTNOHAND;
- cond_resched();
- }
- }
- return i;
-}
-
-/*
- * compat_copy_strings() is basically a copy of copy_strings() from fs/exec.c
- * except that it works with 32 bit argv and envp pointers.
- */
-static int compat_copy_strings(int argc, compat_uptr_t __user *argv,
- struct linux_binprm *bprm)
-{
- struct page *kmapped_page = NULL;
- char *kaddr = NULL;
- unsigned long kpos = 0;
- int ret;
-
- while (argc-- > 0) {
- compat_uptr_t str;
- int len;
- unsigned long pos;
-
- if (get_user(str, argv+argc) ||
- !(len = strnlen_user(compat_ptr(str), MAX_ARG_STRLEN))) {
- ret = -EFAULT;
- goto out;
- }
-
- if (len > MAX_ARG_STRLEN) {
- ret = -E2BIG;
- goto out;
- }
-
- /* We're going to work our way backwords. */
- pos = bprm->p;
- str += len;
- bprm->p -= len;
-
- while (len > 0) {
- int offset, bytes_to_copy;
-
- if (fatal_signal_pending(current)) {
- ret = -ERESTARTNOHAND;
- goto out;
- }
- cond_resched();
-
- offset = pos % PAGE_SIZE;
- if (offset == 0)
- offset = PAGE_SIZE;
-
- bytes_to_copy = offset;
- if (bytes_to_copy > len)
- bytes_to_copy = len;
-
- offset -= bytes_to_copy;
- pos -= bytes_to_copy;
- str -= bytes_to_copy;
- len -= bytes_to_copy;
-
- if (!kmapped_page || kpos != (pos & PAGE_MASK)) {
- struct page *page;
-
- page = get_arg_page(bprm, pos, 1);
- if (!page) {
- ret = -E2BIG;
- goto out;
- }
-
- if (kmapped_page) {
- flush_kernel_dcache_page(kmapped_page);
- kunmap(kmapped_page);
- put_page(kmapped_page);
- }
- kmapped_page = page;
- kaddr = kmap(kmapped_page);
- kpos = pos & PAGE_MASK;
- flush_cache_page(bprm->vma, kpos,
- page_to_pfn(kmapped_page));
- }
- if (copy_from_user(kaddr+offset, compat_ptr(str),
- bytes_to_copy)) {
- ret = -EFAULT;
- goto out;
- }
- }
- }
- ret = 0;
-out:
- if (kmapped_page) {
- flush_kernel_dcache_page(kmapped_page);
- kunmap(kmapped_page);
- put_page(kmapped_page);
- }
- return ret;
-}
-
-/*
- * compat_do_execve() is mostly a copy of do_execve(), with the exception
- * that it processes 32 bit argv and envp pointers.
- */
-int compat_do_execve(char * filename,
- compat_uptr_t __user *argv,
- compat_uptr_t __user *envp,
- struct pt_regs * regs)
-{
- struct linux_binprm *bprm;
- struct file *file;
- struct files_struct *displaced;
- bool clear_in_exec;
- int retval;
-
- retval = unshare_files(&displaced);
- if (retval)
- goto out_ret;
-
- retval = -ENOMEM;
- bprm = kzalloc(sizeof(*bprm), GFP_KERNEL);
- if (!bprm)
- goto out_files;
-
- retval = prepare_bprm_creds(bprm);
- if (retval)
- goto out_free;
-
- retval = check_unsafe_exec(bprm);
- if (retval < 0)
- goto out_free;
- clear_in_exec = retval;
- current->in_execve = 1;
-
- file = open_exec(filename);
- retval = PTR_ERR(file);
- if (IS_ERR(file))
- goto out_unmark;
-
- sched_exec();
-
- bprm->file = file;
- bprm->filename = filename;
- bprm->interp = filename;
-
- retval = bprm_mm_init(bprm);
- if (retval)
- goto out_file;
-
- bprm->argc = compat_count(argv, MAX_ARG_STRINGS);
- if ((retval = bprm->argc) < 0)
- goto out;
-
- bprm->envc = compat_count(envp, MAX_ARG_STRINGS);
- if ((retval = bprm->envc) < 0)
- goto out;
-
- retval = prepare_binprm(bprm);
- if (retval < 0)
- goto out;
-
- retval = copy_strings_kernel(1, &bprm->filename, bprm);
- if (retval < 0)
- goto out;
-
- bprm->exec = bprm->p;
- retval = compat_copy_strings(bprm->envc, envp, bprm);
- if (retval < 0)
- goto out;
-
- retval = compat_copy_strings(bprm->argc, argv, bprm);
- if (retval < 0)
- goto out;
-
- retval = search_binary_handler(bprm, regs);
- if (retval < 0)
- goto out;
-
- /* execve succeeded */
- current->fs->in_exec = 0;
- current->in_execve = 0;
- acct_update_integrals(current);
- free_bprm(bprm);
- if (displaced)
- put_files_struct(displaced);
- return retval;
-
-out:
- if (bprm->mm) {
- acct_arg_size(bprm, 0);
- mmput(bprm->mm);
- }
-
-out_file:
- if (bprm->file) {
- allow_write_access(bprm->file);
- fput(bprm->file);
- }
-
-out_unmark:
- if (clear_in_exec)
- current->fs->in_exec = 0;
- current->in_execve = 0;
-
-out_free:
- free_bprm(bprm);
-
-out_files:
- if (displaced)
- reset_files_struct(displaced);
-out_ret:
- return retval;
-}
-
#define __COMPAT_NFDBITS (8 * sizeof(compat_ulong_t))
static int poll_select_copy_remaining(struct timespec *end_time, void __user *p,
next prev parent reply other threads:[~2011-03-05 20:40 UTC|newest]
Thread overview: 109+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-10-25 3:26 [resend][PATCH 1/4] oom: remove totalpage normalization from oom_badness() KOSAKI Motohiro
2010-10-25 3:27 ` [resend][PATCH 2/4] Revert "oom: deprecate oom_adj tunable" KOSAKI Motohiro
2010-10-25 20:40 ` David Rientjes
2010-10-26 13:01 ` KOSAKI Motohiro
2010-10-26 19:37 ` David Rientjes
2010-11-01 7:06 ` KOSAKI Motohiro
2010-11-01 19:36 ` David Rientjes
2010-11-09 2:26 ` KOSAKI Motohiro
2010-11-09 3:28 ` KOSAKI Motohiro
2010-11-15 0:24 ` KOSAKI Motohiro
2010-11-15 9:59 ` David Rientjes
2010-11-09 23:33 ` David Rientjes
2010-11-09 23:35 ` Alan Cox
2010-11-09 23:48 ` David Rientjes
2010-11-09 23:55 ` [patch] oom: document obsolete oom_adj tunable David Rientjes
2010-11-15 0:22 ` KOSAKI Motohiro
2010-11-15 10:38 ` David Rientjes
2010-11-23 7:16 ` KOSAKI Motohiro
2010-11-14 5:07 ` [resend][PATCH 2/4] Revert "oom: deprecate oom_adj tunable" KOSAKI Motohiro
2010-11-14 21:39 ` David Rientjes
2010-11-23 7:16 ` KOSAKI Motohiro
2010-11-28 1:41 ` David Rientjes
2010-11-30 13:03 ` KOSAKI Motohiro
2010-11-30 20:07 ` David Rientjes
2010-10-25 3:28 ` [resend][PATCH 3/4] move cred_guard_mutex from task_struct to signal_struct KOSAKI Motohiro
2010-10-25 17:26 ` Roland McGrath
2010-10-25 17:42 ` Oleg Nesterov
2010-10-25 17:51 ` Roland McGrath
2010-10-26 13:04 ` KOSAKI Motohiro
2010-10-26 13:18 ` Roland McGrath
2010-10-25 3:29 ` [resend][PATCH 4/4] oom: don't ignore rss in nascent mm KOSAKI Motohiro
2010-10-25 11:28 ` pageexec
2010-10-26 7:25 ` KOSAKI Motohiro
2010-11-23 14:34 ` Oleg Nesterov
2010-11-24 0:24 ` KOSAKI Motohiro
2010-11-24 11:09 ` Oleg Nesterov
2010-11-25 11:06 ` KOSAKI Motohiro
2010-11-25 14:02 ` Oleg Nesterov
2010-11-25 19:36 ` Oleg Nesterov
2010-11-29 5:25 ` KOSAKI Motohiro
2010-11-29 11:33 ` Oleg Nesterov
2010-11-29 18:23 ` Oleg Nesterov
2010-11-30 19:54 ` [PATCH 0/2] exec: more excessive argument size fixes for 2.6.37/stable Oleg Nesterov
2010-11-30 19:55 ` [PATCH 1/2] exec: make argv/envp memory visible to oom-killer Oleg Nesterov
2010-12-01 0:12 ` KOSAKI Motohiro
2010-12-01 18:07 ` Oleg Nesterov
2010-11-30 19:56 ` [PATCH 2/2] exec: copy-and-paste the fixes into compat_do_execve() paths Oleg Nesterov
2010-12-01 3:04 ` KOSAKI Motohiro
2010-11-30 20:00 ` [PATCH 0/4] exec: unify compat/non-compat code Oleg Nesterov
2010-11-30 20:00 ` [PATCH 1/4] exec: introduce get_arg_ptr() helper Oleg Nesterov
2010-11-30 20:01 ` [PATCH 2/4] exec: introduce "bool compat" argument Oleg Nesterov
2010-11-30 20:01 ` [PATCH 3/4] exec: unify compat_do_execve() code Oleg Nesterov
2010-12-01 17:37 ` (No subject header) Milton Miller
2010-12-01 18:27 ` Oleg Nesterov
2011-02-25 17:52 ` [PATCH 0/4 RESEND] exec: unify compat/non-compat code Oleg Nesterov
2011-02-25 17:52 ` [PATCH 1/5] exec: introduce get_arg_ptr() helper Oleg Nesterov
2011-02-25 17:52 ` [PATCH 2/5] exec: introduce "bool compat" argument Oleg Nesterov
2011-02-25 18:57 ` Linus Torvalds
2011-02-26 12:37 ` Oleg Nesterov
2011-02-25 17:53 ` [PATCH 3/5] exec: unify compat_do_execve() code Oleg Nesterov
2011-02-25 19:10 ` Linus Torvalds
2011-02-26 12:37 ` Oleg Nesterov
2011-02-26 12:57 ` Oleg Nesterov
2011-02-26 15:55 ` Linus Torvalds
2011-02-26 17:44 ` Oleg Nesterov
2011-03-01 20:47 ` [PATCH v2 0/5] exec: unify native/compat code Oleg Nesterov
2011-03-01 20:48 ` [PATCH v2 1/5] exec: introduce get_arg_ptr() helper Oleg Nesterov
2011-03-01 20:48 ` [PATCH v2 2/5] exec: introduce "bool compat" argument Oleg Nesterov
2011-03-01 20:48 ` [PATCH v2 3/5] exec: introduce conditional_user_ptr_t Oleg Nesterov
2011-03-01 20:49 ` [PATCH v2 4/5] exec: unify do_execve/compat_do_execve code Oleg Nesterov
2011-03-01 20:49 ` [PATCH v2 5/5] exec: document acct_arg_size() Oleg Nesterov
2011-03-01 21:39 ` [PATCH v2 0/5] exec: unify native/compat code Linus Torvalds
2011-03-02 16:26 ` [PATCH v3 0/4] " Oleg Nesterov
2011-03-02 16:27 ` [PATCH v3 1/4] exec: introduce get_arg_ptr() helper Oleg Nesterov
2011-03-03 3:01 ` KOSAKI Motohiro
2011-03-03 15:47 ` Oleg Nesterov
2011-03-03 16:07 ` Linus Torvalds
2011-03-05 20:30 ` [PATCH v4 0/4] exec: unify native/compat code Oleg Nesterov
2011-03-05 20:31 ` [PATCH v4 1/4] exec: introduce get_user_arg_ptr() helper Oleg Nesterov
2011-03-05 20:31 ` [PATCH v4 2/4] exec: introduce struct user_arg_ptr Oleg Nesterov
2011-03-05 20:31 ` Oleg Nesterov [this message]
2011-03-05 20:52 ` [PATCH v4 3/4] exec: unify do_execve/compat_do_execve code Linus Torvalds
2011-03-05 21:20 ` Oleg Nesterov
2011-03-05 20:31 ` [PATCH v4 4/4] exec: document acct_arg_size() Oleg Nesterov
2011-03-06 12:04 ` [PATCH v4 0/4] exec: unify native/compat code KOSAKI Motohiro
2011-03-06 17:01 ` [PATCH v5 " Oleg Nesterov
2011-03-06 17:02 ` [PATCH v5 1/4] exec: introduce get_user_arg_ptr() helper Oleg Nesterov
2011-03-06 17:02 ` [PATCH v5 2/4] exec: introduce struct user_arg_ptr Oleg Nesterov
2011-03-06 17:02 ` [PATCH v5 3/4] exec: unify do_execve/compat_do_execve code Oleg Nesterov
2011-03-06 17:03 ` [PATCH v5 4/4] exec: document acct_arg_size() Oleg Nesterov
2011-03-02 16:27 ` [PATCH v3 2/4] exec: introduce struct conditional_ptr Oleg Nesterov
2011-03-03 3:08 ` KOSAKI Motohiro
2011-03-02 16:27 ` [PATCH v3 3/4] exec: unify do_execve/compat_do_execve code Oleg Nesterov
2011-03-03 3:13 ` KOSAKI Motohiro
2011-03-02 16:28 ` [PATCH v3 4/4] exec: document acct_arg_size() Oleg Nesterov
2011-03-03 3:09 ` KOSAKI Motohiro
2011-03-02 16:44 ` [PATCH v3 0/4] exec: unify native/compat code Oleg Nesterov
2011-03-02 18:00 ` Linus Torvalds
2011-03-02 19:40 ` David Miller
2011-03-02 19:48 ` Linus Torvalds
2011-03-02 19:54 ` David Miller
2011-02-25 17:53 ` [PATCH 4/5] exec: unexport acct_arg_size() and get_arg_page() Oleg Nesterov
2011-02-25 17:54 ` [PATCH 5/5] exec: document acct_arg_size() Oleg Nesterov
2011-02-25 18:54 ` [PATCH 0/4 RESEND] exec: unify compat/non-compat code Linus Torvalds
2011-02-26 12:35 ` Oleg Nesterov
2010-11-30 20:01 ` [PATCH 4/4] exec: unexport acct_arg_size() and get_arg_page() Oleg Nesterov
2010-12-01 3:09 ` [PATCH 0/4] exec: unify compat/non-compat code KOSAKI Motohiro
2010-11-30 0:06 ` [resend][PATCH 4/4] oom: don't ignore rss in nascent mm KOSAKI Motohiro
2010-10-25 20:37 ` [resend][PATCH 1/4] oom: remove totalpage normalization from oom_badness() David Rientjes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110305203140.GD7546@redhat.com \
--to=oleg@redhat.com \
--cc=akpm@linux-foundation.org \
--cc=eteo@redhat.com \
--cc=kosaki.motohiro@jp.fujitsu.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=miltonm@bga.com \
--cc=pageexec@freemail.hu \
--cc=roland@redhat.com \
--cc=solar@openwall.com \
--cc=spender@grsecurity.net \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).