Subject: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

could someone kindly advise me on the location of some example code in
the kernel which calls one of the userspace system calls from inside the
kernel?

alternatively if this has never been considered before, please could
someone advise me as to how it might be achieved?

thank you,

l.

[p.s. i found asm/unistd.h, i found the macros syscall012345
etc., i believe i don't quite understand what these are for, and
may be on the wrong track.]

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />


2004-10-08 13:16:14

by Fabiano Ramos

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, 2004-10-08 at 14:04 +0100, Luke Kenneth Casson Leighton wrote:
> could someone kindly advise me on the location of some example code in
> the kernel which calls one of the userspace system calls from inside the
> kernel?
>
> alternatively if this has never been considered before, please could
> someone advise me as to how it might be achieved?
>

you cannot do that. For every sys_xx there is a do_xx, that can
be called from inside the kernel.

> thank you,
>
> l.
>
> [p.s. i found asm/unistd.h, i found the macros syscall012345
> etc., i believe i don't quite understand what these are for, and
> may be on the wrong track.]

These are are available for you to make syscalls from user mode
without library support (usually that brand new syscall you added).
They are basically wrappers that expand into C code. _syscallx,
where x is the number of arguments the syscall needs.

>

2004-10-08 13:41:29

by Brice Goglin

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

> For every sys_xx there is a do_xx, that can
> be called from inside the kernel.

Well, not every sys_xx, but most of them :)
For example there's no do_epoll_ctl for sys_epoll_ctl.
I requested this one a long time ago but didn't get it.

Regards,

Brice Goglin
================================================
Ph.D Student
Laboratoire de l'Informatique et du Parall?lisme
CNRS-ENS Lyon-INRIA-UCB Lyon

2004-10-08 14:02:09

by Brian Gerst

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

Luke Kenneth Casson Leighton wrote:
> could someone kindly advise me on the location of some example code in
> the kernel which calls one of the userspace system calls from inside the
> kernel?
>
> alternatively if this has never been considered before, please could
> someone advise me as to how it might be achieved?

What are you trying to do? In most cases needing to use syscalls from
within the kernel is an indication of a design flaw. The most common
case is loading firmware, which should use request_firmware() instead.

--
Brian Gerst

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 10:02:08AM -0400, Brian Gerst wrote:
> Luke Kenneth Casson Leighton wrote:
> >could someone kindly advise me on the location of some example code in
> >the kernel which calls one of the userspace system calls from inside the
> >kernel?
> >
> >alternatively if this has never been considered before, please could
> >someone advise me as to how it might be achieved?
>
> What are you trying to do?

call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
etc. - everything that does file access.

> In most cases needing to use syscalls from
> within the kernel is an indication of a design flaw.

in this case it's an attempt to avoid cutting and pasting
the entire contents of sys_rename, sys_pread, sys_this,
sys_that, removing the first couple and last few lines (that
do copy_from_user) and replacing the arguments with either
a dentry or a kernel-side char* instead of an __user char*.

my alternative is to patch every single vfs-related sys_* in fs/*.c to
be able to "plug in" to these functions.

l.

2004-10-08 15:13:05

by Bernd Petrovitsch

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, 2004-10-08 at 16:18 +0100, Luke Kenneth Casson Leighton wrote:
> On Fri, Oct 08, 2004 at 10:02:08AM -0400, Brian Gerst wrote:
> > Luke Kenneth Casson Leighton wrote:
> > >could someone kindly advise me on the location of some example code in
> > >the kernel which calls one of the userspace system calls from inside the
> > >kernel?
> > >
> > >alternatively if this has never been considered before, please could
> > >someone advise me as to how it might be achieved?
> >
> > What are you trying to do?
>
> call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
> etc. - everything that does file access.
>
> > In most cases needing to use syscalls from
> > within the kernel is an indication of a design flaw.
>
> in this case it's an attempt to avoid cutting and pasting
> the entire contents of sys_rename, sys_pread, sys_this,
> sys_that, removing the first couple and last few lines (that
> do copy_from_user) and replacing the arguments with either
> a dentry or a kernel-side char* instead of an __user char*.
>
> my alternative is to patch every single vfs-related sys_* in fs/*.c to
> be able to "plug in" to these functions.

Why not implement it in user-space?

Bernd
--
Firmix Software GmbH http://www.firmix.at/
mobil: +43 664 4416156 fax: +43 1 7890849-55
Embedded Linux Development and Services

2004-10-08 15:20:14

by Brice Goglin

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

> call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
> etc. - everything that does file access.

If you ever actually call sys_this or sys_that ... from
the kernel, you'll have to do something like this to avoid
copy_from/to_user to fail because the target buffer is not
in kernel space:

mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
<do you stuff here>
set_fs(old_fs);

Just look for set_fs in the kernel source to find examples.
--
Brice Goglin
================================================
Ph.D Student
Laboratoire de l'Informatique et du Parall?lisme
CNRS-ENS Lyon-INRIA-UCB Lyon
France

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 10:07:04AM -0300, Fabiano Ramos wrote:
> On Fri, 2004-10-08 at 14:04 +0100, Luke Kenneth Casson Leighton wrote:
> > could someone kindly advise me on the location of some example code in
> > the kernel which calls one of the userspace system calls from inside the
> > kernel?
> >
> > alternatively if this has never been considered before, please could
> > someone advise me as to how it might be achieved?
> >
>
> you cannot do that. For every sys_xx there is a do_xx, that can
> be called from inside the kernel.

so, there's a do_rename (yes i found that and ISTRC that when
i used it i can't exactly remember what the problem was:
either i got an error code -14 or i got "warning symbol
do_rename not found" when my module was linked together,
even though it says EXPORT_SYMBOL(do_rename) in fs/namei.c,
so i was forced to cut/paste sys_rename)

and there's a do_open no there isn't, there's filp_open.

and a do_pread64 no there isn't i had to cut/paste sys_pread64
which was okay because it's pretty basic, just call vfs_read.

and a do_mkdir no there isn't so i had to cut/paste that.


basically what i am doing is writing a file system "proxy"
module which re-calls back into the filesystem with a prefix
onto the front of the pathname.

> > [p.s. i found asm/unistd.h, i found the macros syscall012345
> > etc., i believe i don't quite understand what these are for, and
> > may be on the wrong track.]
>
> These are are available for you to make syscalls from user mode
> without library support (usually that brand new syscall you added).
> They are basically wrappers that expand into C code. _syscallx,
> where x is the number of arguments the syscall needs.

so, it's for use the other way round. okay, thanks for keeping me off
a broken line of enquiry.

[oh, and i'll be abandoning this line of enquiry _entirely_ if i find
that supermount-ng can do the same job - namely manage to keep
userspace programs happy when users rip out media]

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />

2004-10-08 15:28:58

by Brian Gerst

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

Luke Kenneth Casson Leighton wrote:
> On Fri, Oct 08, 2004 at 10:02:08AM -0400, Brian Gerst wrote:
>
>>Luke Kenneth Casson Leighton wrote:
>>
>>>could someone kindly advise me on the location of some example code in
>>>the kernel which calls one of the userspace system calls from inside the
>>>kernel?
>>>
>>>alternatively if this has never been considered before, please could
>>>someone advise me as to how it might be achieved?
>>
>>What are you trying to do?
>
>
> call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
> etc. - everything that does file access.
>

Why? What are you trying to do that cannot be done in userspace?

--
Brian Gerst

2004-10-08 15:38:03

by Fabiano Ramos

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, 2004-10-08 at 15:38 +0200, Brice Goglin wrote:
> > For every sys_xx there is a do_xx, that can
> > be called from inside the kernel.
>
> Well, not every sys_xx, but most of them :)

yes, you're right. ptrace() is another example.
Too quick of an answer from may part ;)


> For example there's no do_epoll_ctl for sys_epoll_ctl.
> I requested this one a long time ago but didn't get it.
>
> Regards,
>
> Brice Goglin
> ================================================
> Ph.D Student
> Laboratoire de l'Informatique et du Parall?lisme
> CNRS-ENS Lyon-INRIA-UCB Lyon
> -
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 05:18:40PM +0200, Brice Goglin wrote:
> > call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
> > etc. - everything that does file access.
>
> If you ever actually call sys_this or sys_that ... from
> the kernel, you'll have to do something like this to avoid
> copy_from/to_user to fail because the target buffer is not
> in kernel space:
>
> mm_segment_t old_fs;
> old_fs = get_fs();
> set_fs(KERNEL_DS);
> <do you stuff here>
> set_fs(old_fs);

that's it! that's what i was looking for. thank you.

l.

2004-10-08 16:37:08

by Jan-Benedict Glaw

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, 2004-10-08 17:20:25 +0100, Luke Kenneth Casson Leighton <[email protected]>
wrote in message <[email protected]>:
> On Fri, Oct 08, 2004 at 05:18:40PM +0200, Brice Goglin wrote:

> > mm_segment_t old_fs;
> > old_fs = get_fs();
> > set_fs(KERNEL_DS);
> > <do you stuff here>
> > set_fs(old_fs);
>
> that's it! that's what i was looking for. thank you.

Most probably, this is not what you were looking for. You just don't
know that yet (-:

MfG, JBG

--
Jan-Benedict Glaw [email protected] . +49-172-7608481 _ O _
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg _ _ O
fuer einen Freien Staat voll Freier B?rger" | im Internet! | im Irak! O O O
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));


Attachments:
(No filename) (795.00 B)
signature.asc (189.00 B)
Digital signature
Download all attachments
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 05:12:51PM +0200, Bernd Petrovitsch wrote:

> > my alternative is to patch every single vfs-related sys_* in fs/*.c to
> > be able to "plug in" to these functions.
>
> Why not implement it in user-space?

that is the base that i am working from (fuse).

the problem comes when adding support to fuse for xattrs, and the
subsequent use of those xattrs for SE/Linux.

security/selinux/hooks.c cannot cope with the -512 response
"please try later" which the fuse module always always always
sends, in order for fuse to give the userspace daemon a chance
to wake up and smell the roses.

... and i sure ain't gonna hack selinux about!

l.

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 11:27:47AM -0400, Brian Gerst wrote:

> >>What are you trying to do?
> >
> >
> > call sys_rename, sys_pread, sys_create, sys_mknod, sys_rmdir
> > etc. - everything that does file access.
> >
>
> Why? What are you trying to do that cannot be done in userspace?

see other message: i'm trying to combine fuse + its example program
fusexmp into a kernelspace module: when i added xattrs i get a -512
pleasetrylater response which selinux cannot cope with.

l.

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, Oct 08, 2004 at 06:37:01PM +0200, Jan-Benedict Glaw wrote:
> On Fri, 2004-10-08 17:20:25 +0100, Luke Kenneth Casson Leighton <[email protected]>
> wrote in message <[email protected]>:
> > On Fri, Oct 08, 2004 at 05:18:40PM +0200, Brice Goglin wrote:
>
> > > mm_segment_t old_fs;
> > > old_fs = get_fs();
> > > set_fs(KERNEL_DS);
> > > <do you stuff here>
> > > set_fs(old_fs);
> >
> > that's it! that's what i was looking for. thank you.
>
> Most probably, this is not what you were looking for. You just don't
> know that yet (-:

*grin*. yeh, like someone else privately responded saying i might want
to look at compat_alloc_userspace() instead :)


2004-10-10 23:13:15

by Aboo Valappil

[permalink] [raw]
Subject: RE: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

Hi,

In the past I looked in to open, read and write a file from a kernel
module. But problem I faced using the kernel function was that it
checks for the permissions of the file and path against the "current"
process. For eg: open_namei() function ... My requirement was to open
the file regardless of the permissions on the file and also not by
modifying task_struct of the current process to change the permissions
first ! I also wanted not associate the file with the current/any
processes.

Any ideas on this ?

Then I thought of using a work around and avoid opening files in kernel
mode.

Thanks

Aboo


-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Luke Kenneth
Casson Leighton
Sent: Saturday, October 09, 2004 1:35 AM
To: Fabiano Ramos
Cc: [email protected]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from
inside kernel

On Fri, Oct 08, 2004 at 10:07:04AM -0300, Fabiano Ramos wrote:
> On Fri, 2004-10-08 at 14:04 +0100, Luke Kenneth Casson Leighton wrote:
> > could someone kindly advise me on the location of some example code
in
> > the kernel which calls one of the userspace system calls from inside
the
> > kernel?
> >
> > alternatively if this has never been considered before, please could
> > someone advise me as to how it might be achieved?
> >
>
> you cannot do that. For every sys_xx there is a do_xx, that can
> be called from inside the kernel.

so, there's a do_rename (yes i found that and ISTRC that when
i used it i can't exactly remember what the problem was:
either i got an error code -14 or i got "warning symbol
do_rename not found" when my module was linked together,
even though it says EXPORT_SYMBOL(do_rename) in fs/namei.c,
so i was forced to cut/paste sys_rename)

and there's a do_open no there isn't, there's filp_open.

and a do_pread64 no there isn't i had to cut/paste sys_pread64
which was okay because it's pretty basic, just call vfs_read.

and a do_mkdir no there isn't so i had to cut/paste that.


basically what i am doing is writing a file system "proxy"
module which re-calls back into the filesystem with a prefix
onto the front of the pathname.

> > [p.s. i found asm/unistd.h, i found the macros syscall012345
> > etc., i believe i don't quite understand what these are for, and
> > may be on the wrong track.]
>
> These are are available for you to make syscalls from user mode
> without library support (usually that brand new syscall you added).
> They are basically wrappers that expand into C code. _syscallx,
> where x is the number of arguments the syscall needs.

so, it's for use the other way round. okay, thanks for keeping me off
a broken line of enquiry.

[oh, and i'll be abandoning this line of enquiry _entirely_ if i find
that supermount-ng can do the same job - namely manage to keep
userspace programs happy when users rip out media]

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />

2004-10-10 23:38:25

by Arnd Bergmann

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Maandag 11 Oktober 2004 01:13, Aboo Valappil wrote:
> I also wanted not associate the file with the current/any
> processes.
>
> Any ideas on this ?
>
> Then I thought of using a work around and avoid opening files in kernel
> mode.

Most of the code that traditionally used to read files from inside the
kernel can be converted to calling request_firmware(). The basic
idea is that you have a user space helper that writes the data into
the kernel instead of the other way round.

Arnd <><


Attachments:
(No filename) (498.00 B)
(No filename) (189.00 B)
signature
Download all attachments

2004-10-11 10:06:38

by Jiri Kosina

[permalink] [raw]
Subject: RE: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Mon, 11 Oct 2004, Aboo Valappil wrote:

> In the past I looked in to open, read and write a file from a kernel
> module. But problem I faced using the kernel function was that it
> checks for the permissions of the file and path against the "current"
> process. For eg: open_namei() function ... My requirement was to open
> the file regardless of the permissions on the file and also not by
> modifying task_struct of the current process to change the permissions
> first ! I also wanted not associate the file with the current/any
> processes. Any ideas on this ?

1) This is offtopic on LKML, kernelnewbies might be more apropriate list
2) look at set_fs() and filp_open() calls, they might be useful for you
3) Think twice if opening files from kernel is the thing you are willing
to do

--
JiKos.

2004-10-12 00:15:41

by Jon Masters

[permalink] [raw]
Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

On Fri, 8 Oct 2004 14:04:42 +0100, Luke Kenneth Casson Leighton
<[email protected]> wrote:
> could someone kindly advise me on the location of some example code in
> the kernel which calls one of the userspace system calls from inside the
> kernel?

Hi Luke,

I enjoyed your recent talk at OxLUG in which you mentioned this
briefly. Could you please send me the source that you are working on
so that I can take a look and make suggestions if tha'ts useful - I
posted to the OxLUG list but you're not actually on it and although I
now have your address from someone, this post reminded me.

I've copied lkml for two reasons - a). Someone else might want to take
a look. b). I sat and talked with Luke for a while about this, he's
not a typical "I want to do stuff in the kernel I should be doing from
userspace" kind of person in my opinion (there might still be a better
way but until I see more what he's actually doing then I can't work
out what).

Jon.

Subject: Re: how do you call userspace syscalls (e.g. sys_rename) from inside kernel

hiya jon,

i'm placing _a_ version at http://hands.com/~lkcl/fuse.xattr.2.tgz.

it's the "mostly userspace" version, with hacked-up xattrs that "track"
what the userspace code does, inside the kernel.

due to having to implement fuse "getxattr" and friends in
kernel-space (in order to avoid having the -512 error message
returned to selinux/hooks.c), there are two locations in the
code where you must specify the proxy point:

1) example/fusexmp.c you must change "/home/sez" to your-directory.

2) kernel/util.c likewise.

in 1) it is the location where userspace file access occurs. 2) is
obviously matching the userspace xattr access.

i _do_ have a version of fuse where
pretty-much-everything-but-the-inode-allocation-which-is-done-in-libfuse-has-been-moved-to-kernel.

i've been examining a number of kernel modules (ncpfs primarily)
in an attempt to understand how to move libfuse's userspace
inode allocation system into kernelspace.

i'm tempted to abandon fuse and use lufs instead... that has a directory
cache in userspace but at least it appears to allocate inodes in
kernel (using iunique()).

hmmm...


On Tue, Oct 12, 2004 at 01:15:34AM +0100, Jon Masters wrote:
> On Fri, 8 Oct 2004 14:04:42 +0100, Luke Kenneth Casson Leighton
> <[email protected]> wrote:
> > could someone kindly advise me on the location of some example code in
> > the kernel which calls one of the userspace system calls from inside the
> > kernel?
>
> Hi Luke,
>
> I enjoyed your recent talk at OxLUG in which you mentioned this
> briefly. Could you please send me the source that you are working on
> so that I can take a look and make suggestions if tha'ts useful - I
> posted to the OxLUG list but you're not actually on it and although I
> now have your address from someone, this post reminded me.
>
> I've copied lkml for two reasons - a). Someone else might want to take
> a look. b). I sat and talked with Luke for a while about this, he's
> not a typical "I want to do stuff in the kernel I should be doing from
> userspace" kind of person in my opinion (there might still be a better
> way but until I see more what he's actually doing then I can't work
> out what).
>
> Jon.

--
--
Truth, honesty and respect are rare commodities that all spring from
the same well: Love. If you love yourself and everyone and everything
around you, funnily and coincidentally enough, life gets a lot better.
--
<a href="http://lkcl.net"> lkcl.net </a> <br />
<a href="mailto:[email protected]"> [email protected] </a> <br />