Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Linux: Kernel

[RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3

 

 

First page Previous page 1 2 Next page Last page  View All Linux kernel RSS feed   Index | Next | Previous | View Threaded


gorcunov at openvz

Mar 8, 2012, 8:51 AM

Post #1 of 48 (489 views)
Permalink
[RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3

Hi Oleg, could you please take a look once you get a minute (no urgency).

Cyrill
---
From: Cyrill Gorcunov <gorcunov [at] openvz>
Subject: c/r: prctl: Add ability to set new mm_struct::exe_file v3

When we do restore we would like to have a way to setup
a former mm_struct::exe_file so that /proc/pid/exe would
point to the original executable file a process had at
checkpoint time.

For this the PR_SET_MM_EXE_FILE code is introduced.
This option takes a file descriptor which will be
set as new /proc/$pid/exe symlink.

This feature is available iif CONFIG_CHECKPOINT_RESTORE is set.

Signed-off-by: Cyrill Gorcunov <gorcunov [at] openvz>
CC: Oleg Nesterov <oleg [at] redhat>
CC: KOSAKI Motohiro <kosaki.motohiro [at] jp>
CC: Pavel Emelyanov <xemul [at] parallels>
CC: Kees Cook <keescook [at] chromium>
CC: Tejun Heo <tj [at] kernel>
---
include/linux/prctl.h | 1 +
kernel/sys.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 51 insertions(+)

Index: linux-2.6.git/include/linux/prctl.h
===================================================================
--- linux-2.6.git.orig/include/linux/prctl.h
+++ linux-2.6.git/include/linux/prctl.h
@@ -118,5 +118,6 @@
# define PR_SET_MM_ENV_START 10
# define PR_SET_MM_ENV_END 11
# define PR_SET_MM_AUXV 12
+# define PR_SET_MM_EXE_FILE 13

#endif /* _LINUX_PRCTL_H */
Index: linux-2.6.git/kernel/sys.c
===================================================================
--- linux-2.6.git.orig/kernel/sys.c
+++ linux-2.6.git/kernel/sys.c
@@ -36,6 +36,8 @@
#include <linux/personality.h>
#include <linux/ptrace.h>
#include <linux/fs_struct.h>
+#include <linux/file.h>
+#include <linux/mount.h>
#include <linux/gfp.h>
#include <linux/syscore_ops.h>
#include <linux/version.h>
@@ -1701,6 +1703,50 @@ static bool vma_flags_mismatch(struct vm
(vma->vm_flags & banned);
}

+static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
+{
+ struct file *exe_file;
+ struct dentry *dentry;
+ int err;
+
+ exe_file = fget(fd);
+ if (!exe_file)
+ return -EBADF;
+
+ dentry = exe_file->f_path.dentry;
+
+ /*
+ * Because the original mm->exe_file
+ * points to executable file, make sure
+ * this one is executable as well to not
+ * break "big" picture and proc/pid/exe
+ * symlink will be still pointing to
+ * executable one.
+ */
+ err = -EACCES;
+ if (!S_ISREG(dentry->d_inode->i_mode) ||
+ exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
+ goto exit;
+
+ err = inode_permission(dentry->d_inode, MAY_EXEC);
+ if (err)
+ goto exit;
+
+ down_write(&mm->mmap_sem);
+ if (mm->num_exe_file_vmas) {
+ fput(mm->exe_file);
+ mm->exe_file = exe_file;
+ exe_file = NULL;
+ } else
+ set_mm_exe_file(mm, exe_file);
+ up_write(&mm->mmap_sem);
+
+exit:
+ if (exe_file)
+ fput(exe_file);
+ return err;
+}
+
static int prctl_set_mm(int opt, unsigned long addr,
unsigned long arg4, unsigned long arg5)
{
@@ -1715,6 +1761,9 @@ static int prctl_set_mm(int opt, unsigne
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

+ if (opt == PR_SET_MM_EXE_FILE)
+ return prctl_set_mm_exe_file(mm, (unsigned int)addr);
+
if (addr >= TASK_SIZE)
return -EINVAL;

@@ -1837,6 +1886,7 @@ static int prctl_set_mm(int opt, unsigne

return 0;
}
+
default:
error = -EINVAL;
goto out;
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


oleg at redhat

Mar 8, 2012, 10:26 AM

Post #2 of 48 (490 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On 03/08, Cyrill Gorcunov wrote:
>
> Hi Oleg, could you please take a look once you get a minute (no urgency).

Add Matt. I won't touch the text below to keep the patch intact.

With this change

down_write(&mm->mmap_sem);
if (mm->num_exe_file_vmas) {
fput(mm->exe_file);
mm->exe_file = exe_file;
exe_file = NULL;
} else
set_mm_exe_file(mm, exe_file);
up_write(&mm->mmap_sem);

I simply do not understand what mm->num_exe_file_vmas means after
PR_SET_MM_EXE_FILE.

I think that you should do

down_write(&mm->mmap_sem);
if (mm->num_exe_file_vmas) {
fput(mm->exe_file);
mm->exe_file = exe_file;
exe_file = NULL;
}
up_write(&mm->mmap_sem);

to keep the current "mm->exe_file goes away after the final
unmap(MAP_EXECUTABLE)" logic.

OK, may be this doesn't work in c/r case because you are actually
going to remove the old mappings? But in this case the new exe_file
will go away anyway, afaics PR_SET_MM_EXE_FILE is called when you
still have the old mappings.

And I don't think the unconditional

down_write(&mm->mmap_sem);
set_mm_exe_file(mm, exe_file);
up_write(&mm->mmap_sem);

is 100% right, this clears ->num_exe_file_vmas. This means that
(if you still have the old mapping) the new exe_file can go away
after added_exe_file_vma() + removed_exe_file_vma(). Normally this
should happen, but afaics this is possible. Note that even, say,
mprotect() can trigger added_exe_file_vma().

May be we can do something like

down_write(&mm->mmap_sem);
set_mm_exe_file(mm, exe_file);
// we are cheating anyway, make sure it can never == 0
// if we have the "old" VM_EXECUTABLE vmas.
mm->num_exe_file_vmas = LONG_MAX;
up_write(&mm->mmap_sem);

I dunno. Matt, could you help?

> From: Cyrill Gorcunov <gorcunov [at] openvz>
> Subject: c/r: prctl: Add ability to set new mm_struct::exe_file v3
>
> When we do restore we would like to have a way to setup
> a former mm_struct::exe_file so that /proc/pid/exe would
> point to the original executable file a process had at
> checkpoint time.
>
> For this the PR_SET_MM_EXE_FILE code is introduced.
> This option takes a file descriptor which will be
> set as new /proc/$pid/exe symlink.
>
> This feature is available iif CONFIG_CHECKPOINT_RESTORE is set.
>
> Signed-off-by: Cyrill Gorcunov <gorcunov [at] openvz>
> CC: Oleg Nesterov <oleg [at] redhat>
> CC: KOSAKI Motohiro <kosaki.motohiro [at] jp>
> CC: Pavel Emelyanov <xemul [at] parallels>
> CC: Kees Cook <keescook [at] chromium>
> CC: Tejun Heo <tj [at] kernel>
> ---
> include/linux/prctl.h | 1 +
> kernel/sys.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 51 insertions(+)
>
> Index: linux-2.6.git/include/linux/prctl.h
> ===================================================================
> --- linux-2.6.git.orig/include/linux/prctl.h
> +++ linux-2.6.git/include/linux/prctl.h
> @@ -118,5 +118,6 @@
> # define PR_SET_MM_ENV_START 10
> # define PR_SET_MM_ENV_END 11
> # define PR_SET_MM_AUXV 12
> +# define PR_SET_MM_EXE_FILE 13
>
> #endif /* _LINUX_PRCTL_H */
> Index: linux-2.6.git/kernel/sys.c
> ===================================================================
> --- linux-2.6.git.orig/kernel/sys.c
> +++ linux-2.6.git/kernel/sys.c
> @@ -36,6 +36,8 @@
> #include <linux/personality.h>
> #include <linux/ptrace.h>
> #include <linux/fs_struct.h>
> +#include <linux/file.h>
> +#include <linux/mount.h>
> #include <linux/gfp.h>
> #include <linux/syscore_ops.h>
> #include <linux/version.h>
> @@ -1701,6 +1703,50 @@ static bool vma_flags_mismatch(struct vm
> (vma->vm_flags & banned);
> }
>
> +static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
> +{
> + struct file *exe_file;
> + struct dentry *dentry;
> + int err;
> +
> + exe_file = fget(fd);
> + if (!exe_file)
> + return -EBADF;
> +
> + dentry = exe_file->f_path.dentry;
> +
> + /*
> + * Because the original mm->exe_file
> + * points to executable file, make sure
> + * this one is executable as well to not
> + * break "big" picture and proc/pid/exe
> + * symlink will be still pointing to
> + * executable one.
> + */
> + err = -EACCES;
> + if (!S_ISREG(dentry->d_inode->i_mode) ||
> + exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
> + goto exit;
> +
> + err = inode_permission(dentry->d_inode, MAY_EXEC);
> + if (err)
> + goto exit;
> +
> + down_write(&mm->mmap_sem);
> + if (mm->num_exe_file_vmas) {
> + fput(mm->exe_file);
> + mm->exe_file = exe_file;
> + exe_file = NULL;
> + } else
> + set_mm_exe_file(mm, exe_file);
> + up_write(&mm->mmap_sem);
> +
> +exit:
> + if (exe_file)
> + fput(exe_file);
> + return err;
> +}
> +
> static int prctl_set_mm(int opt, unsigned long addr,
> unsigned long arg4, unsigned long arg5)
> {
> @@ -1715,6 +1761,9 @@ static int prctl_set_mm(int opt, unsigne
> if (!capable(CAP_SYS_RESOURCE))
> return -EPERM;
>
> + if (opt == PR_SET_MM_EXE_FILE)
> + return prctl_set_mm_exe_file(mm, (unsigned int)addr);
> +
> if (addr >= TASK_SIZE)
> return -EINVAL;
>
> @@ -1837,6 +1886,7 @@ static int prctl_set_mm(int opt, unsigne
>
> return 0;
> }
> +
> default:
> error = -EINVAL;
> goto out;

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 11:03 AM

Post #3 of 48 (487 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 07:26:23PM +0100, Oleg Nesterov wrote:
> On 03/08, Cyrill Gorcunov wrote:
> >
> > Hi Oleg, could you please take a look once you get a minute (no urgency).
>
> Add Matt. I won't touch the text below to keep the patch intact.

Thanks for CC'ing Matt, Oleg (I forgot, sorry).

>
> With this change
>
> down_write(&mm->mmap_sem);
> if (mm->num_exe_file_vmas) {
> fput(mm->exe_file);
> mm->exe_file = exe_file;
> exe_file = NULL;
> } else
> set_mm_exe_file(mm, exe_file);
> up_write(&mm->mmap_sem);
>
> I simply do not understand what mm->num_exe_file_vmas means after
> PR_SET_MM_EXE_FILE.
>
> I think that you should do
>
> down_write(&mm->mmap_sem);
> if (mm->num_exe_file_vmas) {
> fput(mm->exe_file);
> mm->exe_file = exe_file;
> exe_file = NULL;
> }
> up_write(&mm->mmap_sem);
>
> to keep the current "mm->exe_file goes away after the final
> unmap(MAP_EXECUTABLE)" logic.
>
> OK, may be this doesn't work in c/r case because you are actually
> going to remove the old mappings? But in this case the new exe_file
> will go away anyway, afaics PR_SET_MM_EXE_FILE is called when you
> still have the old mappings.

Yes, exactly, I need to remove old mappings first (because VMAs
we're about to restore may intersect with current map the host
program has). And yes, once they all are removed I don't have
/proc/pid/exe anymore. That's why I need num_exe_file_vmas == 0
case.

When I setup new exe_file with num_exe_file_vmas = 0, this reference
to a file brings /proc/pid/exe back to live (and when process exiting
it'll call set_mm_exe_file(mm, NULL) and the new exe_file will be dropped,
so no leak here).

>
> And I don't think the unconditional
>
> down_write(&mm->mmap_sem);
> set_mm_exe_file(mm, exe_file);
> up_write(&mm->mmap_sem);
>
> is 100% right, this clears ->num_exe_file_vmas. This means that
> (if you still have the old mapping) the new exe_file can go away
> after added_exe_file_vma() + removed_exe_file_vma(). Normally this
> should happen, but afaics this is possible. Note that even, say,
> mprotect() can trigger added_exe_file_vma().
>

Wait, Oleg, I'm confused, in case if there *is* exitsting VM_EXECUTABLEs
then we jump into first banch and simply replace old exe_file.
If there is no VM_EXECUTABLEs, then we simply setup new exe_file
and num_exe_file_vmas remains zero.

Or I miss something obvious and we somehow can cause the kernel
to map VM_EXECUTABLEs out of binfmt-elf loader?

> May be we can do something like
>
> down_write(&mm->mmap_sem);
> set_mm_exe_file(mm, exe_file);
> // we are cheating anyway, make sure it can never == 0
> // if we have the "old" VM_EXECUTABLE vmas.
> mm->num_exe_file_vmas = LONG_MAX;
> up_write(&mm->mmap_sem);
>
> I dunno. Matt, could you help?

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


oleg at redhat

Mar 8, 2012, 11:05 AM

Post #4 of 48 (492 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On 03/08, Cyrill Gorcunov wrote:
>
> On Thu, Mar 08, 2012 at 07:26:23PM +0100, Oleg Nesterov wrote:
> > I think that you should do
> >
> > down_write(&mm->mmap_sem);
> > if (mm->num_exe_file_vmas) {
> > fput(mm->exe_file);
> > mm->exe_file = exe_file;
> > exe_file = NULL;
> > }
> > up_write(&mm->mmap_sem);
> >
> > to keep the current "mm->exe_file goes away after the final
> > unmap(MAP_EXECUTABLE)" logic.
> >
> > OK, may be this doesn't work in c/r case because you are actually
> > going to remove the old mappings? But in this case the new exe_file
> > will go away anyway, afaics PR_SET_MM_EXE_FILE is called when you
> > still have the old mappings.
>
> Yes, exactly, I need to remove old mappings first (because VMAs
> we're about to restore may intersect with current map the host
> program has). And yes, once they all are removed I don't have
> /proc/pid/exe anymore. That's why I need num_exe_file_vmas == 0
> case.

OK, in this case PR_SET_MM_EXE_FILE should probably fail if
mm->num_exe_file_vmas != 0 ? This way it would be more or less
consistent or at least understandable. Just we add the new
special case: num_exe_file_vmas == 0 but exe_file != NULL
because c/r people are crazy.

> > And I don't think the unconditional
> >
> > down_write(&mm->mmap_sem);
> > set_mm_exe_file(mm, exe_file);
> > up_write(&mm->mmap_sem);
> >
> > is 100% right, this clears ->num_exe_file_vmas. This means that
> > (if you still have the old mapping) the new exe_file can go away
> > after added_exe_file_vma() + removed_exe_file_vma(). Normally this
> > should happen, but afaics this is possible. Note that even, say,
> > mprotect() can trigger added_exe_file_vma().
> >
>
> Wait, Oleg, I'm confused, in case if there *is* exitsting VM_EXECUTABLEs
> then we jump into first banch and simply replace old exe_file.

Yes. And then later you remove the old mapping (which do not match
the new file anyway) and the new exe_file goes away. Unlikely you
want this.

Oleg.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 11:25 AM

Post #5 of 48 (488 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 08:05:34PM +0100, Oleg Nesterov wrote:
...
> >
> > Yes, exactly, I need to remove old mappings first (because VMAs
> > we're about to restore may intersect with current map the host
> > program has). And yes, once they all are removed I don't have
> > /proc/pid/exe anymore. That's why I need num_exe_file_vmas == 0
> > case.
>
> OK, in this case PR_SET_MM_EXE_FILE should probably fail if
> mm->num_exe_file_vmas != 0 ? This way it would be more or less
> consistent or at least understandable. Just we add the new
> special case: num_exe_file_vmas == 0 but exe_file != NULL
> because c/r people are crazy.
>

Sure, I can drop num_exe_file_vmas != 0 case and refuse to
setup new exe symlink if there some VM_EXECUTABLE remains
unmapped. Sounds good?

> > > And I don't think the unconditional
> > >
> > > down_write(&mm->mmap_sem);
> > > set_mm_exe_file(mm, exe_file);
> > > up_write(&mm->mmap_sem);
> > >
> > > is 100% right, this clears ->num_exe_file_vmas. This means that
> > > (if you still have the old mapping) the new exe_file can go away
> > > after added_exe_file_vma() + removed_exe_file_vma(). Normally this
> > > should happen, but afaics this is possible. Note that even, say,
> > > mprotect() can trigger added_exe_file_vma().
> > >
> >
> > Wait, Oleg, I'm confused, in case if there *is* exitsting VM_EXECUTABLEs
> > then we jump into first banch and simply replace old exe_file.
>
> Yes. And then later you remove the old mapping (which do not match
> the new file anyway) and the new exe_file goes away. Unlikely you
> want this.

Yes, unlikely ;)

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


oleg at redhat

Mar 8, 2012, 11:25 AM

Post #6 of 48 (487 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On 03/08, Cyrill Gorcunov wrote:
>
> On Thu, Mar 08, 2012 at 08:05:34PM +0100, Oleg Nesterov wrote:
> ...
> > >
> > > Yes, exactly, I need to remove old mappings first (because VMAs
> > > we're about to restore may intersect with current map the host
> > > program has). And yes, once they all are removed I don't have
> > > /proc/pid/exe anymore. That's why I need num_exe_file_vmas == 0
> > > case.
> >
> > OK, in this case PR_SET_MM_EXE_FILE should probably fail if
> > mm->num_exe_file_vmas != 0 ? This way it would be more or less
> > consistent or at least understandable. Just we add the new
> > special case: num_exe_file_vmas == 0 but exe_file != NULL
> > because c/r people are crazy.
> >
>
> Sure, I can drop num_exe_file_vmas != 0 case and refuse to
> setup new exe symlink if there some VM_EXECUTABLE remains
> unmapped. Sounds good?

Personally I like this. This is simple and _understable_, even
if ->num_exe_file_vmas has no meaning after PR_SET_MM_EXE.

But please-please document the new special case in the changelog.

Oleg.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


keescook at chromium

Mar 8, 2012, 11:31 AM

Post #7 of 48 (491 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 8:51 AM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> When we do restore we would like to have a way to setup
> a former mm_struct::exe_file so that /proc/pid/exe would
> point to the original executable file a process had at
> checkpoint time.
>
> For this the PR_SET_MM_EXE_FILE code is introduced.
> This option takes a file descriptor which will be
> set as new /proc/$pid/exe symlink.
>
> This feature is available iif CONFIG_CHECKPOINT_RESTORE is set.
> [...]
> Index: linux-2.6.git/kernel/sys.c
> ===================================================================
> --- linux-2.6.git.orig/kernel/sys.c
> +++ linux-2.6.git/kernel/sys.c
> [...]
> +       exe_file = fget(fd);
> +       if (!exe_file)
> +               return -EBADF;
> +
> +       dentry = exe_file->f_path.dentry;
> +
> +       /*
> +        * Because the original mm->exe_file
> +        * points to executable file, make sure
> +        * this one is executable as well to not
> +        * break "big" picture and proc/pid/exe
> +        * symlink will be still pointing to
> +        * executable one.
> +        */
> +       err = -EACCES;
> +       if (!S_ISREG(dentry->d_inode->i_mode)   ||
> +           exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
> +               goto exit;

I'm starting to notice that this pattern (testing ISREG and
MNT_NOEXEC) is getting repeated a few times in the kernel, and at
least the no-new-privs patch (not yet in -mm but hopefully soon given
the seccomp_filter work) updates this pattern everywhere. Perhaps this
should be extracted into a helper first, and then this patch can call
that helper here? (And then nnp can just update the single helper.)

-Kees

--
Kees Cook
ChromeOS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 11:36 AM

Post #8 of 48 (489 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 08:25:59PM +0100, Oleg Nesterov wrote:
> >
> > Sure, I can drop num_exe_file_vmas != 0 case and refuse to
> > setup new exe symlink if there some VM_EXECUTABLE remains
> > unmapped. Sounds good?
>
> Personally I like this. This is simple and _understable_, even
> if ->num_exe_file_vmas has no meaning after PR_SET_MM_EXE.
>
> But please-please document the new special case in the changelog.
>

Sure, will update, thanks.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 11:40 AM

Post #9 of 48 (494 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 11:31:58AM -0800, Kees Cook wrote:
...
> > +       err = -EACCES;
> > +       if (!S_ISREG(dentry->d_inode->i_mode)   ||
> > +           exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
> > +               goto exit;
>
> I'm starting to notice that this pattern (testing ISREG and
> MNT_NOEXEC) is getting repeated a few times in the kernel, and at
> least the no-new-privs patch (not yet in -mm but hopefully soon given
> the seccomp_filter work) updates this pattern everywhere. Perhaps this
> should be extracted into a helper first, and then this patch can call
> that helper here? (And then nnp can just update the single helper.)
>

I can do that if Andrew agree.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


luto at amacapital

Mar 8, 2012, 12:02 PM

Post #10 of 48 (490 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 11:40 AM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> On Thu, Mar 08, 2012 at 11:31:58AM -0800, Kees Cook wrote:
> ...
>> > +       err = -EACCES;
>> > +       if (!S_ISREG(dentry->d_inode->i_mode)   ||
>> > +           exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
>> > +               goto exit;
>>
>> I'm starting to notice that this pattern (testing ISREG and
>> MNT_NOEXEC) is getting repeated a few times in the kernel, and at
>> least the no-new-privs patch (not yet in -mm but hopefully soon given
>> the seccomp_filter work) updates this pattern everywhere. Perhaps this
>> should be extracted into a helper first, and then this patch can call
>> that helper here? (And then nnp can just update the single helper.)
>>
>
> I can do that if Andrew agree.

I'm a bit lost. nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
checks. (And the effects of the two flags is different in selinux for
historical reasons.) I'm sure I'm missing something.

--Andy

>
>        Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


keescook at chromium

Mar 8, 2012, 12:06 PM

Post #11 of 48 (489 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 12:02 PM, Andy Lutomirski <luto [at] amacapital> wrote:
> On Thu, Mar 8, 2012 at 11:40 AM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
>> On Thu, Mar 08, 2012 at 11:31:58AM -0800, Kees Cook wrote:
>> ...
>>> > +       err = -EACCES;
>>> > +       if (!S_ISREG(dentry->d_inode->i_mode)   ||
>>> > +           exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
>>> > +               goto exit;
>>>
>>> I'm starting to notice that this pattern (testing ISREG and
>>> MNT_NOEXEC) is getting repeated a few times in the kernel, and at
>>> least the no-new-privs patch (not yet in -mm but hopefully soon given
>>> the seccomp_filter work) updates this pattern everywhere. Perhaps this
>>> should be extracted into a helper first, and then this patch can call
>>> that helper here? (And then nnp can just update the single helper.)
>>>
>>
>> I can do that if Andrew agree.
>
> I'm a bit lost.  nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
> checks.  (And the effects of the two flags is different in selinux for
> historical reasons.)  I'm sure I'm missing something.

Oops, you're right. Regardless, we might want helpers anyway. Better
to have single places to do these tests.

-Kees

--
Kees Cook
ChromeOS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 12:07 PM

Post #12 of 48 (491 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 12:02:50PM -0800, Andy Lutomirski wrote:
> >
> > I can do that if Andrew agree.
>
> I'm a bit lost. nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
> checks. (And the effects of the two flags is different in selinux for
> historical reasons.) I'm sure I'm missing something.
>

Andy, I've no idea what nnp is ;) I was only about to gather those
ISREG/MNT_NOEXEC to one helper since we indeed have a few places in
kernel which do same thing in open-coded manner.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


luto at amacapital

Mar 8, 2012, 12:15 PM

Post #13 of 48 (494 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 12:07 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> On Thu, Mar 08, 2012 at 12:02:50PM -0800, Andy Lutomirski wrote:
>> >
>> > I can do that if Andrew agree.
>>
>> I'm a bit lost.  nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
>> checks.  (And the effects of the two flags is different in selinux for
>> historical reasons.)  I'm sure I'm missing something.
>>
>
> Andy, I've no idea what nnp is ;) I was only about to gather those
> ISREG/MNT_NOEXEC to one helper since we indeed have a few places in
> kernel which do same thing in open-coded manner.

Am I not the Andrew you were referring to?

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 12:21 PM

Post #14 of 48 (476 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 12:15:55PM -0800, Andy Lutomirski wrote:
> On Thu, Mar 8, 2012 at 12:07 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> > On Thu, Mar 08, 2012 at 12:02:50PM -0800, Andy Lutomirski wrote:
> >> >
> >> > I can do that if Andrew agree.
> >>
> >> I'm a bit lost.  nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
> >> checks.  (And the effects of the two flags is different in selinux for
> >> historical reasons.)  I'm sure I'm missing something.
> >>
> >
> > Andy, I've no idea what nnp is ;) I was only about to gather those
> > ISREG/MNT_NOEXEC to one helper since we indeed have a few places in
> > kernel which do same thing in open-coded manner.
>
> Am I not the Andrew you were referring to?
>

Nope, I meant Andrew Morton /because this patch is for -mm/ ;)

I've been in To: field that's why I replied you about nnp
(and, btw, what nnp is? not "Net national product" I suppose,
this hint wikipedia gave me)

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


luto at amacapital

Mar 8, 2012, 12:24 PM

Post #15 of 48 (476 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 12:21 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> On Thu, Mar 08, 2012 at 12:15:55PM -0800, Andy Lutomirski wrote:
>> On Thu, Mar 8, 2012 at 12:07 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
>> > On Thu, Mar 08, 2012 at 12:02:50PM -0800, Andy Lutomirski wrote:
>> >> >
>> >> > I can do that if Andrew agree.
>> >>
>> >> I'm a bit lost.  nnp updates the MNT_NOSUID checks, not the MNT_NOEXEC
>> >> checks.  (And the effects of the two flags is different in selinux for
>> >> historical reasons.)  I'm sure I'm missing something.
>> >>
>> >
>> > Andy, I've no idea what nnp is ;) I was only about to gather those
>> > ISREG/MNT_NOEXEC to one helper since we indeed have a few places in
>> > kernel which do same thing in open-coded manner.
>>
>> Am I not the Andrew you were referring to?
>>
>
> Nope, I meant Andrew Morton /because this patch is for -mm/ ;)
>
> I've been in To: field that's why I replied you about nnp
> (and, btw, what nnp is? not "Net national product" I suppose,
>  this hint wikipedia gave me)

nnp is no_new_privs, which is my patch and is almost, but not quite,
very relevant to this discussion. Hence my confusion ;)

FWIW, since I've touched this code recently, the cleanup you're
suggesting sounds good.

--Andy
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 12:28 PM

Post #16 of 48 (478 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 12:24:38PM -0800, Andy Lutomirski wrote:
...
> >
> > I've been in To: field that's why I replied you about nnp
> > (and, btw, what nnp is? not "Net national product" I suppose,
> >  this hint wikipedia gave me)
>
> nnp is no_new_privs, which is my patch and is almost, but not quite,
> very relevant to this discussion. Hence my confusion ;)

Ah, good to know (I'm on 3.3-rc6 now, so I've not yet noticed
this nnp helper :)

>
> FWIW, since I've touched this code recently, the cleanup you're
> suggesting sounds good.

OK, once I prepare the helper we will see how it fits with
other code.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 1:48 PM

Post #17 of 48 (472 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 08:25:59PM +0100, Oleg Nesterov wrote:
...
>
> But please-please document the new special case in the changelog.
>

Oleg, will the following change log sound more or less fine?

Cyrill
---
From: Cyrill Gorcunov <gorcunov [at] openvz>
Subject: c/r: prctl: Add ability to set new mm_struct::exe_file v4

When we do restore we would like to have a way to setup
a former mm_struct::exe_file so that /proc/pid/exe would
point to the original executable file a process had at
checkpoint time.

For this the PR_SET_MM_EXE_FILE code is introduced.
This option takes a file descriptor which will be
set as a source for new /proc/$pid/exe symlink.

Note it allows to change /proc/$pid/exe iif there
are no VM_EXECUTABLE vmas present for current process,
simply because this feature is a special to C/R
and mm::num_exe_file_vmas become meaningless after
that.

This feature is available iif CONFIG_CHECKPOINT_RESTORE is set.

Signed-off-by: Cyrill Gorcunov <gorcunov [at] openvz>
CC: Oleg Nesterov <oleg [at] redhat>
CC: KOSAKI Motohiro <kosaki.motohiro [at] jp>
CC: Pavel Emelyanov <xemul [at] parallels>
CC: Kees Cook <keescook [at] chromium>
CC: Tejun Heo <tj [at] kernel>
CC: Matt Helsley <matthltc [at] us>
---
include/linux/prctl.h | 1
kernel/sys.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 55 insertions(+)

Index: linux-2.6.git/include/linux/prctl.h
===================================================================
--- linux-2.6.git.orig/include/linux/prctl.h
+++ linux-2.6.git/include/linux/prctl.h
@@ -118,5 +118,6 @@
# define PR_SET_MM_ENV_START 10
# define PR_SET_MM_ENV_END 11
# define PR_SET_MM_AUXV 12
+# define PR_SET_MM_EXE_FILE 13

#endif /* _LINUX_PRCTL_H */
Index: linux-2.6.git/kernel/sys.c
===================================================================
--- linux-2.6.git.orig/kernel/sys.c
+++ linux-2.6.git/kernel/sys.c
@@ -36,6 +36,8 @@
#include <linux/personality.h>
#include <linux/ptrace.h>
#include <linux/fs_struct.h>
+#include <linux/file.h>
+#include <linux/mount.h>
#include <linux/gfp.h>
#include <linux/syscore_ops.h>
#include <linux/version.h>
@@ -1701,6 +1703,55 @@ static bool vma_flags_mismatch(struct vm
(vma->vm_flags & banned);
}

+static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
+{
+ struct file *exe_file;
+ struct dentry *dentry;
+ int err;
+
+ exe_file = fget(fd);
+ if (!exe_file)
+ return -EBADF;
+
+ dentry = exe_file->f_path.dentry;
+
+ /*
+ * Because the original mm->exe_file
+ * points to executable file, make sure
+ * this one is executable as well to not
+ * break an overall picture.
+ */
+ err = -EACCES;
+ if (!S_ISREG(dentry->d_inode->i_mode) ||
+ exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
+ goto exit;
+
+ err = inode_permission(dentry->d_inode, MAY_EXEC);
+ if (err)
+ goto exit;
+
+ /*
+ * Setting new mm::exe_file is only allowed
+ * when no executable VMAs left. This is
+ * special C/R case when a restored program
+ * need to change own /proc/$pid/exe symlink.
+ * After this call mm::num_exe_file_vmas become
+ * meaningless.
+ */
+ down_write(&mm->mmap_sem);
+ if (mm->num_exe_file_vmas == 0) {
+ set_mm_exe_file(mm, exe_file);
+ exe_file = NULL;
+ } else
+ err = -EBUSY;
+ up_write(&mm->mmap_sem);
+
+exit:
+ if (exe_file)
+ fput(exe_file);
+ return err;
+}
+
static int prctl_set_mm(int opt, unsigned long addr,
unsigned long arg4, unsigned long arg5)
{
@@ -1715,6 +1766,9 @@ static int prctl_set_mm(int opt, unsigne
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

+ if (opt == PR_SET_MM_EXE_FILE)
+ return prctl_set_mm_exe_file(mm, (unsigned int)addr);
+
if (addr >= TASK_SIZE)
return -EINVAL;

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 1:57 PM

Post #18 of 48 (471 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 12:24:38PM -0800, Andy Lutomirski wrote:
>
> nnp is no_new_privs, which is my patch and is almost, but not quite,
> very relevant to this discussion. Hence my confusion ;)
>
> FWIW, since I've touched this code recently, the cleanup you're
> suggesting sounds good.
>

Andy, Kees, I guess the patch below might be a helper we need,
while I'm not sure on naming. hm?

Cyrill
---
include/linux/fs.h | 6 ++++++
1 file changed, 6 insertions(+)

Index: linux-2.6.git/include/linux/fs.h
===================================================================
--- linux-2.6.git.orig/include/linux/fs.h
+++ linux-2.6.git/include/linux/fs.h
@@ -2669,5 +2669,11 @@ static inline void inode_has_no_xattr(st
inode->i_flags |= S_NOSEC;
}

+static inline bool file_may_exec(struct file *f)
+{
+ return S_ISREG(f->f_path.dentry->d_inode->i_mode) &&
+ !(f->f_path.mnt->mnt_flags & MNT_NOEXEC);
+}
+
#endif /* __KERNEL__ */
#endif /* _LINUX_FS_H */
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


keescook at chromium

Mar 8, 2012, 2:03 PM

Post #19 of 48 (475 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 1:57 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> On Thu, Mar 08, 2012 at 12:24:38PM -0800, Andy Lutomirski wrote:
>>
>> nnp is no_new_privs, which is my patch and is almost, but not quite,
>> very relevant to this discussion.  Hence my confusion ;)
>>
>> FWIW, since I've touched this code recently, the cleanup you're
>> suggesting sounds good.
>>
>
> Andy, Kees, I guess the patch below might be a helper we need,
> while I'm not sure on naming. hm?
>
>        Cyrill
> ---
>  include/linux/fs.h |    6 ++++++
>  1 file changed, 6 insertions(+)
>
> Index: linux-2.6.git/include/linux/fs.h
> ===================================================================
> --- linux-2.6.git.orig/include/linux/fs.h
> +++ linux-2.6.git/include/linux/fs.h
> @@ -2669,5 +2669,11 @@ static inline void inode_has_no_xattr(st
>                inode->i_flags |= S_NOSEC;
>  }
>
> +static inline bool file_may_exec(struct file *f)
> +{
> +       return S_ISREG(f->f_path.dentry->d_inode->i_mode) &&
> +               !(f->f_path.mnt->mnt_flags & MNT_NOEXEC);
> +}
> +
>  #endif /* __KERNEL__ */
>  #endif /* _LINUX_FS_H */

How about "file_is_exec" instead, since it doesn't (and likely
shouldn't) include the inode_permission(..., EXEC)? I'd like other
people's thoughts on this since maybe it's not needed and I instead
have accidentally derailed this patch with useless bike shedding.

-Kees

--
Kees Cook
ChromeOS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 8, 2012, 2:12 PM

Post #20 of 48 (479 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 08, 2012 at 02:03:13PM -0800, Kees Cook wrote:
>
> How about "file_is_exec" instead, since it doesn't (and likely
> shouldn't) include the inode_permission(..., EXEC)? I'd like other
> people's thoughts on this since maybe it's not needed and I instead
> have accidentally derailed this patch with useless bike shedding.
>

Yup. Anyway, I'm leaving the former mm_struct::exe_file patch with
EXEC test opencoded so we can do everything on top then.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


keescook at chromium

Mar 8, 2012, 2:14 PM

Post #21 of 48 (474 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Thu, Mar 8, 2012 at 2:12 PM, Cyrill Gorcunov <gorcunov [at] openvz> wrote:
> On Thu, Mar 08, 2012 at 02:03:13PM -0800, Kees Cook wrote:
>>
>> How about "file_is_exec" instead, since it doesn't (and likely
>> shouldn't) include the inode_permission(..., EXEC)? I'd like other
>> people's thoughts on this since maybe it's not needed and I instead
>> have accidentally derailed this patch with useless bike shedding.
>>
>
> Yup. Anyway, I'm leaving the former mm_struct::exe_file patch with
> EXEC test opencoded so we can do everything on top then.

Sounds good to me. Thanks for putting up with my distraction! :)

-Kees

--
Kees Cook
ChromeOS Security
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


oleg at redhat

Mar 9, 2012, 4:48 AM

Post #22 of 48 (464 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On 03/09, Cyrill Gorcunov wrote:
>
> +static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
> +{
> + struct file *exe_file;
> + struct dentry *dentry;
> + int err;
> +
> + exe_file = fget(fd);
> + if (!exe_file)
> + return -EBADF;
> +
> + dentry = exe_file->f_path.dentry;
> +
> + /*
> + * Because the original mm->exe_file
> + * points to executable file, make sure
> + * this one is executable as well to not
> + * break an overall picture.
> + */
> + err = -EACCES;
> + if (!S_ISREG(dentry->d_inode->i_mode) ||
> + exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
> + goto exit;
> +
> + err = inode_permission(dentry->d_inode, MAY_EXEC);
> + if (err)
> + goto exit;
> +
> + /*
> + * Setting new mm::exe_file is only allowed
> + * when no executable VMAs left. This is
^^^^^^^^^^
Perhaps this is just me, but imho "executable" is not clear enough.
I'd suggest VM_EXECUTABLE to avoid the confusion with VM_EXEC.

> + * special C/R case when a restored program
> + * need to change own /proc/$pid/exe symlink.
> + * After this call mm::num_exe_file_vmas become
> + * meaningless.
> + */
> + down_write(&mm->mmap_sem);
> + if (mm->num_exe_file_vmas == 0) {

You can check this at the very start lockless and simplify the code.
Once it is zero, it can never grow (or we have a bug anyway).

> + set_mm_exe_file(mm, exe_file);
> + exe_file = NULL;
> + } else
> + err = -EBUSY;
> + up_write(&mm->mmap_sem);
> +
> +exit:
> + if (exe_file)
> + fput(exe_file);

This doesn't look correct, you need fput() in any case.
set_mm_exe_file() does another get_file().

Oleg.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 9, 2012, 4:57 AM

Post #23 of 48 (467 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Fri, Mar 09, 2012 at 01:48:11PM +0100, Oleg Nesterov wrote:
...
> > +
> > + /*
> > + * Setting new mm::exe_file is only allowed
> > + * when no executable VMAs left. This is
> ^^^^^^^^^^
> Perhaps this is just me, but imho "executable" is not clear enough.
> I'd suggest VM_EXECUTABLE to avoid the confusion with VM_EXEC.

OK

>
> > + * special C/R case when a restored program
> > + * need to change own /proc/$pid/exe symlink.
> > + * After this call mm::num_exe_file_vmas become
> > + * meaningless.
> > + */
> > + down_write(&mm->mmap_sem);
> > + if (mm->num_exe_file_vmas == 0) {
>
> You can check this at the very start lockless and simplify the code.
> Once it is zero, it can never grow (or we have a bug anyway).

sure

>
> > + set_mm_exe_file(mm, exe_file);
> > + exe_file = NULL;
> > + } else
> > + err = -EBUSY;
> > + up_write(&mm->mmap_sem);
> > +
> > +exit:
> > + if (exe_file)
> > + fput(exe_file);
>
> This doesn't look correct, you need fput() in any case.
> set_mm_exe_file() does another get_file().

yeah, thanks, will update.

Cyrill
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


gorcunov at openvz

Mar 9, 2012, 5:35 AM

Post #24 of 48 (465 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On Fri, Mar 09, 2012 at 04:57:35PM +0400, Cyrill Gorcunov wrote:
>
> yeah, thanks, will update.
>

This one should fit all requirements I hope.

Cyrill
---
From: Cyrill Gorcunov <gorcunov [at] openvz>
Subject: c/r: prctl: Add ability to set new mm_struct::exe_file v5

When we do restore we would like to have a way to setup
a former mm_struct::exe_file so that /proc/pid/exe would
point to the original executable file a process had at
checkpoint time.

For this the PR_SET_MM_EXE_FILE code is introduced.
This option takes a file descriptor which will be
set as a source for new /proc/$pid/exe symlink.

Note it allows to change /proc/$pid/exe iif there
are no VM_EXECUTABLE vmas present for current process,
simply because this feature is a special to C/R
and mm::num_exe_file_vmas become meaningless after
that.

This feature is available iif CONFIG_CHECKPOINT_RESTORE is set.

Signed-off-by: Cyrill Gorcunov <gorcunov [at] openvz>
CC: Oleg Nesterov <oleg [at] redhat>
CC: KOSAKI Motohiro <kosaki.motohiro [at] jp>
CC: Pavel Emelyanov <xemul [at] parallels>
CC: Kees Cook <keescook [at] chromium>
CC: Tejun Heo <tj [at] kernel>
CC: Matt Helsley <matthltc [at] us>
---
include/linux/prctl.h | 1
kernel/sys.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 57 insertions(+)

Index: linux-2.6.git/include/linux/prctl.h
===================================================================
--- linux-2.6.git.orig/include/linux/prctl.h
+++ linux-2.6.git/include/linux/prctl.h
@@ -118,5 +118,6 @@
# define PR_SET_MM_ENV_START 10
# define PR_SET_MM_ENV_END 11
# define PR_SET_MM_AUXV 12
+# define PR_SET_MM_EXE_FILE 13

#endif /* _LINUX_PRCTL_H */
Index: linux-2.6.git/kernel/sys.c
===================================================================
--- linux-2.6.git.orig/kernel/sys.c
+++ linux-2.6.git/kernel/sys.c
@@ -36,6 +36,8 @@
#include <linux/personality.h>
#include <linux/ptrace.h>
#include <linux/fs_struct.h>
+#include <linux/file.h>
+#include <linux/mount.h>
#include <linux/gfp.h>
#include <linux/syscore_ops.h>
#include <linux/version.h>
@@ -1701,6 +1703,57 @@ static bool vma_flags_mismatch(struct vm
(vma->vm_flags & banned);
}

+static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
+{
+ struct file *exe_file;
+ struct dentry *dentry;
+ int err;
+
+ if (mm->num_exe_file_vmas)
+ return -EBUSY;
+
+ exe_file = fget(fd);
+ if (!exe_file)
+ return -EBADF;
+
+ dentry = exe_file->f_path.dentry;
+
+ /*
+ * Because the original mm->exe_file
+ * points to executable file, make sure
+ * this one is executable as well to not
+ * break an overall picture.
+ */
+ err = -EACCES;
+ if (!S_ISREG(dentry->d_inode->i_mode) ||
+ exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
+ goto exit;
+
+ err = inode_permission(dentry->d_inode, MAY_EXEC);
+ if (err)
+ goto exit;
+
+ /*
+ * Setting new mm::exe_file is only allowed
+ * when no VM_EXECUTABLE vma's left. This is
+ * a special C/R case when a restored program
+ * need to change own /proc/$pid/exe symlink.
+ * After this call mm::num_exe_file_vmas become
+ * meaningless. If mm::num_exe_file_vmas will
+ * ever increase back from zero -- this code
+ * needs to be revised, thus WARN_ here, just
+ * to be sure.
+ */
+ down_write(&mm->mmap_sem);
+ WARN_ON_ONCE(mm->num_exe_file_vmas);
+ set_mm_exe_file(mm, exe_file);
+ up_write(&mm->mmap_sem);
+
+exit:
+ fput(exe_file);
+ return err;
+}
+
static int prctl_set_mm(int opt, unsigned long addr,
unsigned long arg4, unsigned long arg5)
{
@@ -1715,6 +1768,9 @@ static int prctl_set_mm(int opt, unsigne
if (!capable(CAP_SYS_RESOURCE))
return -EPERM;

+ if (opt == PR_SET_MM_EXE_FILE)
+ return prctl_set_mm_exe_file(mm, (unsigned int)addr);
+
if (addr >= TASK_SIZE)
return -EINVAL;

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/


oleg at redhat

Mar 9, 2012, 5:47 AM

Post #25 of 48 (466 views)
Permalink
Re: [RFC] c/r: prctl: Add ability to set new mm_struct::exe_file v3 [In reply to]

On 03/09, Cyrill Gorcunov wrote:
>
> On Fri, Mar 09, 2012 at 04:57:35PM +0400, Cyrill Gorcunov wrote:
> >
> > yeah, thanks, will update.
> >
>
> This one should fit all requirements I hope.

Oh, sorry Cyrill, I simply can't resist...

> +static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
> +{
> + struct file *exe_file;
> + struct dentry *dentry;
> + int err;
> +
> + if (mm->num_exe_file_vmas)
> + return -EBUSY;
> +
> + exe_file = fget(fd);
> + if (!exe_file)
> + return -EBADF;
> +
> + dentry = exe_file->f_path.dentry;
> +
> + /*
> + * Because the original mm->exe_file
> + * points to executable file, make sure
> + * this one is executable as well to not
> + * break an overall picture.
> + */
> + err = -EACCES;
> + if (!S_ISREG(dentry->d_inode->i_mode) ||
> + exe_file->f_path.mnt->mnt_flags & MNT_NOEXEC)
> + goto exit;
> +
> + err = inode_permission(dentry->d_inode, MAY_EXEC);
> + if (err)
> + goto exit;
> +
> + /*
> + * Setting new mm::exe_file is only allowed
> + * when no VM_EXECUTABLE vma's left. This is
> + * a special C/R case when a restored program
> + * need to change own /proc/$pid/exe symlink.
> + * After this call mm::num_exe_file_vmas become
> + * meaningless. If mm::num_exe_file_vmas will
> + * ever increase back from zero -- this code
> + * needs to be revised, thus WARN_ here, just
> + * to be sure.

To be shure in what??

> + */
> + down_write(&mm->mmap_sem);
> + WARN_ON_ONCE(mm->num_exe_file_vmas);

We already checked it is zero. Yes, it shouldn't grow. But why
do we need another check here?

If it can grow, it can grow after we drop mmap_sem as well and
this would be wrong. So may be we need another WARN_ON() at the
end?

I'd understand if you add something like

WARN_ON(!mm->num_exe_file_vmas && !current->in_exec);

into added_exe_file_vma().

Or

WARN_ON(mm->num_exe_file_vmas <= 0);

into removed_exe_file_vma().

But imho your WARN looks like "OK, I checked it lockless but I
am not sure this is correct".

Oleg.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo [at] vger
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

First page Previous page 1 2 Next page Last page  View All Linux kernel RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.