Patchwork [2/5] linux-user: add open() hijack infrastructure

login
register
mail settings
Submitter Alexander Graf
Date Nov. 2, 2011, 7:23 p.m.
Message ID <1320261806-13194-3-git-send-email-agraf@suse.de>
Download mbox | patch
Permalink /patch/123319/
State New
Headers show

Comments

Alexander Graf - Nov. 2, 2011, 7:23 p.m.
There are a number of files in /proc that expose host information
to the guest program. This patch adds infrastructure to override
the open() syscall for guest programs to enable us to on the fly
generate guest sensible files.

Signed-off-by: Alexander Graf <agraf@suse.de>
---
 linux-user/syscall.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 49 insertions(+), 3 deletions(-)
David Gilbert - Nov. 3, 2011, 9:34 a.m.
On 2 November 2011 19:23, Alexander Graf <agraf@suse.de> wrote:
> There are a number of files in /proc that expose host information
> to the guest program. This patch adds infrastructure to override
> the open() syscall for guest programs to enable us to on the fly
> generate guest sensible files.
>
> Signed-off-by: Alexander Graf <agraf@suse.de>
> ---
>  linux-user/syscall.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++--
>  1 files changed, 49 insertions(+), 3 deletions(-)
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 9f5da36..38953ba 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -4600,6 +4600,52 @@ int get_osversion(void)
>     return osversion;
>  }
>
> +static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)

Once you open the pandoras-box that is emulating /proc, I think you'll probably
need to hook it in more places and be more general; although you may
well get away
with it for this particular case.

Isn't it better to put the filename interception code somewhere more general
so that it can also be misused by other calls - e.g. stat().

I guess you're also going to need to be able to do /proc/pid/* instead
of /proc/self;
something is bound to use that.

Dave
Alexander Graf - Nov. 3, 2011, 6:33 p.m.
On 03.11.2011, at 02:34, David Gilbert <david.gilbert@linaro.org> wrote:

> On 2 November 2011 19:23, Alexander Graf <agraf@suse.de> wrote:
>> There are a number of files in /proc that expose host information
>> to the guest program. This patch adds infrastructure to override
>> the open() syscall for guest programs to enable us to on the fly
>> generate guest sensible files.
>> 
>> Signed-off-by: Alexander Graf <agraf@suse.de>
>> ---
>>  linux-user/syscall.c |   52 +++++++++++++++++++++++++++++++++++++++++++++++--
>>  1 files changed, 49 insertions(+), 3 deletions(-)
>> 
>> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
>> index 9f5da36..38953ba 100644
>> --- a/linux-user/syscall.c
>> +++ b/linux-user/syscall.c
>> @@ -4600,6 +4600,52 @@ int get_osversion(void)
>>     return osversion;
>>  }
>> 
>> +static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
> 
> Once you open the pandoras-box that is emulating /proc, I think you'll probably
> need to hook it in more places and be more general; although you may
> well get away
> with it for this particular case.
> 
> Isn't it better to put the filename interception code somewhere more general
> so that it can also be misused by other calls - e.g. stat().

We can move it when we get there :).

> 
> I guess you're also going to need to be able to do /proc/pid/* instead
> of /proc/self;
> something is bound to use that.

I was hoping to get away with a bit less involving. Sure, we could actually open the pandora box of emulating all of /proc, but I really don't want to go down that route.

Since all these files already do exist on the host view, I don't think we have to fake stat(). The file size will be wrong either way.

So yes, eventually we might have to use the big sledge hammer of emulating all of /proc, but as long as I can refrain from it I will :). It's just a _lot_ of work.

Alex

> 
> Dave

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index 9f5da36..38953ba 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -4600,6 +4600,52 @@  int get_osversion(void)
     return osversion;
 }
 
+static int do_open(void *cpu_env, const char *pathname, int flags, mode_t mode)
+{
+    struct fake_open {
+        const char *filename;
+        int (*fill)(void *cpu_env, int fd);
+    };
+    const struct fake_open *fake_open;
+    static const struct fake_open fakes[] = {
+        { NULL, NULL }
+    };
+
+    for (fake_open = fakes; fake_open->filename; fake_open++) {
+        if (!strncmp(pathname, fake_open->filename,
+                     strlen(fake_open->filename))) {
+            break;
+        }
+    }
+
+    if (fake_open->filename) {
+        const char *tmpdir;
+        char filename[PATH_MAX];
+        int fd, r;
+
+        /* create temporary file to map stat to */
+        tmpdir = getenv("TMPDIR");
+        if (!tmpdir)
+            tmpdir = "/tmp";
+        snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
+        fd = mkstemp(filename);
+        if (fd < 0) {
+            return fd;
+        }
+        unlink(filename);
+
+        if ((r = fake_open->fill(cpu_env, fd))) {
+            close(fd);
+            return r;
+        }
+        lseek(fd, 0, SEEK_SET);
+
+        return fd;
+    }
+
+    return get_errno(open(path(pathname), flags, mode));
+}
+
 /* do_syscall() should always have a single exit point at the end so
    that actions, such as logging of syscall results, can be performed.
    All errnos that do_syscall() returns must be -TARGET_<errcode>. */
@@ -4685,9 +4731,9 @@  abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
     case TARGET_NR_open:
         if (!(p = lock_user_string(arg1)))
             goto efault;
-        ret = get_errno(open(path(p),
-                             target_to_host_bitmask(arg2, fcntl_flags_tbl),
-                             arg3));
+        ret = get_errno(do_open(cpu_env, p,
+                                target_to_host_bitmask(arg2, fcntl_flags_tbl),
+                                arg3));
         unlock_user(p, arg1, 0);
         break;
 #if defined(TARGET_NR_openat) && defined(__NR_openat)