Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 1 | #include "defs.h" |
| 2 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 3 | /* these constants are the same as in <linux/capability.h> */ |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 4 | enum { |
| 5 | CAP_CHOWN, |
| 6 | CAP_DAC_OVERRIDE, |
| 7 | CAP_DAC_READ_SEARCH, |
| 8 | CAP_FOWNER, |
| 9 | CAP_FSETID, |
| 10 | CAP_KILL, |
| 11 | CAP_SETGID, |
| 12 | CAP_SETUID, |
| 13 | CAP_SETPCAP, |
| 14 | CAP_LINUX_IMMUTABLE, |
| 15 | CAP_NET_BIND_SERVICE, |
| 16 | CAP_NET_BROADCAST, |
| 17 | CAP_NET_ADMIN, |
| 18 | CAP_NET_RAW, |
| 19 | CAP_IPC_LOCK, |
| 20 | CAP_IPC_OWNER, |
| 21 | CAP_SYS_MODULE, |
| 22 | CAP_SYS_RAWIO, |
| 23 | CAP_SYS_CHROOT, |
| 24 | CAP_SYS_PTRACE, |
| 25 | CAP_SYS_PACCT, |
| 26 | CAP_SYS_ADMIN, |
| 27 | CAP_SYS_BOOT, |
| 28 | CAP_SYS_NICE, |
| 29 | CAP_SYS_RESOURCE, |
| 30 | CAP_SYS_TIME, |
| 31 | CAP_SYS_TTY_CONFIG, |
| 32 | CAP_MKNOD, |
| 33 | CAP_LEASE, |
| 34 | CAP_AUDIT_WRITE, |
| 35 | CAP_AUDIT_CONTROL, |
| 36 | CAP_SETFCAP |
| 37 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 38 | |
| 39 | #include "xlat/capabilities.h" |
| 40 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 41 | /* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */ |
| 42 | enum { |
| 43 | CAP_MAC_OVERRIDE, |
| 44 | CAP_MAC_ADMIN, |
| 45 | CAP_SYSLOG, |
| 46 | CAP_WAKE_ALARM, |
| 47 | CAP_BLOCK_SUSPEND, |
| 48 | CAP_AUDIT_READ |
| 49 | }; |
| 50 | |
| 51 | #include "xlat/capabilities1.h" |
| 52 | |
| 53 | /* these constants are the same as in <linux/capability.h> */ |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 54 | enum { |
| 55 | _LINUX_CAPABILITY_VERSION_1 = 0x19980330, |
| 56 | _LINUX_CAPABILITY_VERSION_2 = 0x20071026, |
| 57 | _LINUX_CAPABILITY_VERSION_3 = 0x20080522 |
| 58 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 59 | |
| 60 | #include "xlat/cap_version.h" |
| 61 | |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 62 | typedef struct user_cap_header_struct { |
| 63 | uint32_t version; |
| 64 | int pid; |
| 65 | } *cap_user_header_t; |
| 66 | |
| 67 | typedef struct user_cap_data_struct { |
| 68 | uint32_t effective; |
| 69 | uint32_t permitted; |
| 70 | uint32_t inheritable; |
| 71 | } *cap_user_data_t; |
| 72 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 73 | static cap_user_header_t |
| 74 | get_cap_header(struct tcb *tcp, unsigned long addr) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 75 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 76 | static struct user_cap_header_struct header; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 77 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 78 | if (!addr || !verbose(tcp)) |
| 79 | return NULL; |
| 80 | |
| 81 | if (umove(tcp, addr, &header) < 0) |
| 82 | return NULL; |
| 83 | |
| 84 | return &header; |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | print_cap_header(struct tcb *tcp, unsigned long addr, cap_user_header_t h) |
| 89 | { |
| 90 | if (!addr) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 91 | tprints("NULL"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
| 95 | if (!h) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 96 | tprintf("%#lx", addr); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 97 | return; |
| 98 | } |
| 99 | |
| 100 | tprints("{"); |
| 101 | printxval(cap_version, h->version, |
| 102 | "_LINUX_CAPABILITY_VERSION_???"); |
| 103 | tprintf(", %d}", h->pid); |
| 104 | } |
| 105 | |
| 106 | static void |
| 107 | print_cap_bits(const uint32_t lo, const uint32_t hi) |
| 108 | { |
| 109 | if (lo || !hi) |
| 110 | printflags(capabilities, lo, "CAP_???"); |
| 111 | |
| 112 | if (hi) { |
| 113 | if (lo) |
| 114 | tprints("|"); |
| 115 | printflags(capabilities1, hi, "CAP_???"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | static void |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 120 | print_cap_data(struct tcb *tcp, unsigned long addr, const cap_user_header_t h) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 121 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 122 | struct user_cap_data_struct data[2]; |
| 123 | unsigned int len; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 124 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 125 | if (!addr) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 126 | tprints("NULL"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 127 | return; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 128 | } |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 129 | |
| 130 | if (!h || !verbose(tcp) || |
| 131 | (exiting(tcp) && syserror(tcp))) { |
| 132 | tprintf("%#lx", addr); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if (_LINUX_CAPABILITY_VERSION_2 == h->version || |
| 137 | _LINUX_CAPABILITY_VERSION_3 == h->version) |
| 138 | len = 2; |
| 139 | else |
| 140 | len = 1; |
| 141 | |
| 142 | if (umoven(tcp, addr, len * sizeof(data[0]), (char *) data) < 0) { |
| 143 | tprintf("%#lx", addr); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | tprints("{"); |
| 148 | print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0); |
| 149 | tprints(", "); |
| 150 | print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0); |
| 151 | tprints(", "); |
| 152 | print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0); |
| 153 | tprints("}"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | int |
| 157 | sys_capget(struct tcb *tcp) |
| 158 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 159 | cap_user_header_t h; |
| 160 | |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 161 | if (entering(tcp)) { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 162 | h = get_cap_header(tcp, tcp->u_arg[0]); |
| 163 | print_cap_header(tcp, tcp->u_arg[0], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 164 | tprints(", "); |
| 165 | } else { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 166 | h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]); |
| 167 | print_cap_data(tcp, tcp->u_arg[1], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 168 | } |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | int |
| 173 | sys_capset(struct tcb *tcp) |
| 174 | { |
| 175 | if (entering(tcp)) { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 176 | cap_user_header_t h = get_cap_header(tcp, tcp->u_arg[0]); |
| 177 | print_cap_header(tcp, tcp->u_arg[0], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 178 | tprints(", "); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 179 | print_cap_data(tcp, tcp->u_arg[1], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 180 | } |
| 181 | return 0; |
| 182 | } |