Skip to contentSkip to conversation

linux-user: Implement capget, capset

PATCH Superseded Peter Maydell 2 participants 3 messages Mar 14, 2014 – Mar 17, 2014

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 linux-user: Implement capget, capset

Implement the capget and capset syscalls. This is useful because simple programs like 'ls' try to use it in AArch64, and otherwise we emit a lot of noise about it being unimplemented.

---
Bugfix or feature? You decide :-)

 linux-user/syscall.c      | 71 +++++++++++++++++++++++++++++++++++++++++++++--
 linux-user/syscall_defs.h | 11 ++++++++
 2 files changed, 80 insertions(+), 2 deletions(-)
linux-user/syscall.c +69 -2
index 2a8b66c..53c3d69 100644
@@ -43,6 +43,7 @@
 #include <sys/resource.h>
 #include <sys/mman.h>
 #include <sys/swap.h>
+#include <sys/capability.h>
 #include <signal.h>
 #include <sched.h>
 #ifdef __ia64__
@@ -7641,9 +7642,75 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         unlock_user(p, arg1, ret);
         break;
     case TARGET_NR_capget:
-        goto unimplemented;
     case TARGET_NR_capset:
-        goto unimplemented;
+    {
+        struct target_user_cap_header *target_header;
+        struct target_user_cap_data *target_data;
+        struct __user_cap_header_struct header;
+        struct __user_cap_data_struct data[2];
+        struct __user_cap_data_struct *dataptr = NULL;
+        int i, target_datalen;
+        int data_items = 1;
+
+        if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
+            goto efault;
+        }
+        header.version = tswap32(target_header->version);
+        header.pid = tswap32(target_header->pid);
+
+        if (header.version != _LINUX_CAPABILITY_VERSION_1) {
+            /* Version 2 and up takes pointer to two user_data structs */
+            data_items = 2;
+        }
+
+        target_datalen = sizeof(*target_data) * data_items;
+
+        if (arg2) {
+            if (num == TARGET_NR_capget) {
+                target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
+            } else {
+                target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
+            }
+            if (!target_data) {
+                unlock_user_struct(target_header, arg1, 0);
+                goto efault;
+            }
+
+            if (num == TARGET_NR_capset) {
+                for (i = 0; i < data_items; i++) {
+                    data[i].effective = tswap32(target_data[i].effective);
+                    data[i].permitted = tswap32(target_data[i].permitted);
+                    data[i].inheritable = tswap32(target_data[i].inheritable);
+                }
+            }
+
+            dataptr = data;
+        }
+
+        if (num == TARGET_NR_capget) {
+            ret = get_errno(capget(&header, dataptr));
+        } else {
+            ret = get_errno(capset(&header, dataptr));
+        }
+
+        /* The kernel always updates version for both capget and capset */
+        target_header->version = tswap32(header.version);
+        unlock_user_struct(target_header, arg1, 1);
+
+        if (arg2) {
+            if (num == TARGET_NR_capget) {
+                for (i = 0; i < data_items; i++) {
+                    target_data[i].effective = tswap32(data[i].effective);
+                    target_data[i].permitted = tswap32(data[i].permitted);
+                    target_data[i].inheritable = tswap32(data[i].inheritable);
+                }
+                unlock_user(target_data, arg2, target_datalen);
+            } else {
+                unlock_user(target_data, arg2, 0);
+            }
+        }
+        break;
+    }
     case TARGET_NR_sigaltstack:
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
linux-user/syscall_defs.h +11 -0
index 732c9e3..7db878a 100644
@@ -2559,3 +2559,14 @@ struct target_sigevent {
         } _sigev_thread;
     } _sigev_un;
 };
+
+struct target_user_cap_header {
+    uint32_t version;
+    int pid;
+};
+
+struct target_user_cap_data {
+    uint32_t effective;
+    uint32_t permitted;
+    uint32_t inheritable;
+};
Signature · 1 line
1.9.0
#1 lore raw
Quoted 2 lines · Peter Maydell

Implement the capget and capset syscalls. This is useful because simple programs like 'ls' try to use it in AArch64

I'm not seing this with ubuntu trusty, wookeys debian or my static busybox. Where is your ls from? Also, runnning qemu-linux user as root? How very brave :)

Quoted 2 lines

, and otherwise
we emit a lot of noise about it being unimplemented.

Well, it seems gcc 4.8 isn't smart enough for this patch:

linux-user/syscall.c: In function ‘do_syscall’:
linux-user/syscall.c:7739:46: error: ‘target_data’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

   target_data[i].effective = tswap32(data[i].effective);
                            ^
Quoted 3 lines

---
Bugfix or feature? You decide :-)

perhaps unimplemented_nowarn for now and a proper implementation for 2.1 ?

Riku

linux-user/syscall.c line 46

Quoted 13 lines
 linux-user/syscall.c      | 71 +++++++++++++++++++++++++++++++++++++++++++++--
 linux-user/syscall_defs.h | 11 ++++++++
 2 files changed, 80 insertions(+), 2 deletions(-)
linux-user/syscall.c +1 -0
index 2a8b66c..53c3d69 100644
@@ -43,6 +43,7 @@
 #include <sys/resource.h>
 #include <sys/mman.h>
 #include <sys/swap.h>
+#include <sys/capability.h>

This is from libcap-dev, which might not be installed by default. The actual capset/capget functions seems to be in libc.

Quoted 102 lines
linux-user/syscall.c +68 -2
 #include <signal.h>
 #include <sched.h>
 #ifdef __ia64__
@@ -7641,9 +7642,75 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
         unlock_user(p, arg1, ret);
         break;
     case TARGET_NR_capget:
-        goto unimplemented;
     case TARGET_NR_capset:
-        goto unimplemented;
+    {
+        struct target_user_cap_header *target_header;
+        struct target_user_cap_data *target_data;
+        struct __user_cap_header_struct header;
+        struct __user_cap_data_struct data[2];
+        struct __user_cap_data_struct *dataptr = NULL;
+        int i, target_datalen;
+        int data_items = 1;
+
+        if (!lock_user_struct(VERIFY_WRITE, target_header, arg1, 1)) {
+            goto efault;
+        }
+        header.version = tswap32(target_header->version);
+        header.pid = tswap32(target_header->pid);
+
+        if (header.version != _LINUX_CAPABILITY_VERSION_1) {
+            /* Version 2 and up takes pointer to two user_data structs */
+            data_items = 2;
+        }
+
+        target_datalen = sizeof(*target_data) * data_items;
+
+        if (arg2) {
+            if (num == TARGET_NR_capget) {
+                target_data = lock_user(VERIFY_WRITE, arg2, target_datalen, 0);
+            } else {
+                target_data = lock_user(VERIFY_READ, arg2, target_datalen, 1);
+            }
+            if (!target_data) {
+                unlock_user_struct(target_header, arg1, 0);
+                goto efault;
+            }
+
+            if (num == TARGET_NR_capset) {
+                for (i = 0; i < data_items; i++) {
+                    data[i].effective = tswap32(target_data[i].effective);
+                    data[i].permitted = tswap32(target_data[i].permitted);
+                    data[i].inheritable = tswap32(target_data[i].inheritable);
+                }
+            }
+
+            dataptr = data;
+        }
+
+        if (num == TARGET_NR_capget) {
+            ret = get_errno(capget(&header, dataptr));
+        } else {
+            ret = get_errno(capset(&header, dataptr));
+        }
+
+        /* The kernel always updates version for both capget and capset */
+        target_header->version = tswap32(header.version);
+        unlock_user_struct(target_header, arg1, 1);
+
+        if (arg2) {
+            if (num == TARGET_NR_capget) {
+                for (i = 0; i < data_items; i++) {
+                    target_data[i].effective = tswap32(data[i].effective);
+                    target_data[i].permitted = tswap32(data[i].permitted);
+                    target_data[i].inheritable = tswap32(data[i].inheritable);
+                }
+                unlock_user(target_data, arg2, target_datalen);
+            } else {
+                unlock_user(target_data, arg2, 0);
+            }
+        }
+        break;
+    }
     case TARGET_NR_sigaltstack:
 #if defined(TARGET_I386) || defined(TARGET_ARM) || defined(TARGET_MIPS) || \
     defined(TARGET_SPARC) || defined(TARGET_PPC) || defined(TARGET_ALPHA) || \
linux-user/syscall_defs.h +11 -0
index 732c9e3..7db878a 100644
@@ -2559,3 +2559,14 @@ struct target_sigevent {
         } _sigev_thread;
     } _sigev_un;
 };
+
+struct target_user_cap_header {
+    uint32_t version;
+    int pid;
+};
+
+struct target_user_cap_data {
+    uint32_t effective;
+    uint32_t permitted;
+    uint32_t inheritable;
+};
Signature · 1 line
1.9.0
#2 lore raw
Quoted 6 lines · Riku Voipio
Quoted 2 lines · Peter Maydell

Implement the capget and capset syscalls. This is useful because simple programs like 'ls' try to use it in AArch64

I'm not seing this with ubuntu trusty, wookeys debian or my static busybox. Where is your ls from?

It's from the SuSE rootfs tarball. (The SuSE patchset wires capget up to nowarn.

Quoted 6 lines

Also, runnning qemu-linux
user as root? How very brave :)

Quoted 2 lines

, and otherwise
we emit a lot of noise about it being unimplemented.

Quoted 7 lines

Well, it seems gcc 4.8 isn't smart enough for this patch:

linux-user/syscall.c: In function ‘do_syscall’:
linux-user/syscall.c:7739:46: error: ‘target_data’ may be used uninitialized in this function [-Werror=maybe-uninitialized]

   target_data[i].effective = tswap32(data[i].effective);
                            ^

Hohum. Let's just stick an = NULL on the target_data definition.

Quoted 5 lines
Quoted 3 lines

---
Bugfix or feature? You decide :-)

perhaps unimplemented_nowarn for now and a proper implementation for 2.1 ?

I guess that might be safer.

Quoted 7 lines

Riku

Quoted 4 lines
 linux-user/syscall.c      | 71 +++++++++++++++++++++++++++++++++++++++++++++--
 linux-user/syscall_defs.h | 11 ++++++++
 2 files changed, 80 insertions(+), 2 deletions(-)
Quoted 12 lines

linux-user/syscall.c line 46

Quoted 9 lines
linux-user/syscall.c +1 -0
index 2a8b66c..53c3d69 100644
@@ -43,6 +43,7 @@
 #include <sys/resource.h>
 #include <sys/mman.h>
 #include <sys/swap.h>
+#include <sys/capability.h>

This is from libcap-dev, which might not be installed by default. The actual capset/capget functions seems to be in libc.

Hmm. Looks like we need to use linux/capability.h and roll our own capget/capset functions then. (We could just provide the prototypes for the functions in libc but since we have a _syscall macro anyway we may as well use it...)

thanks
-- PMM