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 { |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 5 | #include "caps0.h" |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 6 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 7 | |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 8 | #include "xlat/cap_mask0.h" |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 9 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 10 | /* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */ |
| 11 | enum { |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 12 | #include "caps1.h" |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 13 | }; |
| 14 | |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 15 | #include "xlat/cap_mask1.h" |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 16 | |
| 17 | /* these constants are the same as in <linux/capability.h> */ |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 18 | enum { |
| 19 | _LINUX_CAPABILITY_VERSION_1 = 0x19980330, |
| 20 | _LINUX_CAPABILITY_VERSION_2 = 0x20071026, |
| 21 | _LINUX_CAPABILITY_VERSION_3 = 0x20080522 |
| 22 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 23 | |
| 24 | #include "xlat/cap_version.h" |
| 25 | |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 26 | typedef struct user_cap_header_struct { |
| 27 | uint32_t version; |
| 28 | int pid; |
| 29 | } *cap_user_header_t; |
| 30 | |
| 31 | typedef struct user_cap_data_struct { |
| 32 | uint32_t effective; |
| 33 | uint32_t permitted; |
| 34 | uint32_t inheritable; |
| 35 | } *cap_user_data_t; |
| 36 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 37 | static cap_user_header_t |
| 38 | get_cap_header(struct tcb *tcp, unsigned long addr) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 39 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 40 | static struct user_cap_header_struct header; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 41 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 42 | if (!addr || !verbose(tcp)) |
| 43 | return NULL; |
| 44 | |
| 45 | if (umove(tcp, addr, &header) < 0) |
| 46 | return NULL; |
| 47 | |
| 48 | return &header; |
| 49 | } |
| 50 | |
| 51 | static void |
| 52 | print_cap_header(struct tcb *tcp, unsigned long addr, cap_user_header_t h) |
| 53 | { |
| 54 | if (!addr) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 55 | tprints("NULL"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (!h) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 60 | tprintf("%#lx", addr); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | tprints("{"); |
| 65 | printxval(cap_version, h->version, |
| 66 | "_LINUX_CAPABILITY_VERSION_???"); |
| 67 | tprintf(", %d}", h->pid); |
| 68 | } |
| 69 | |
| 70 | static void |
| 71 | print_cap_bits(const uint32_t lo, const uint32_t hi) |
| 72 | { |
| 73 | if (lo || !hi) |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 74 | printflags(cap_mask0, lo, "CAP_???"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 75 | |
| 76 | if (hi) { |
| 77 | if (lo) |
| 78 | tprints("|"); |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 79 | printflags(cap_mask1, hi, "CAP_???"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | static void |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 84 | 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] | 85 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 86 | struct user_cap_data_struct data[2]; |
| 87 | unsigned int len; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 88 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 89 | if (!addr) { |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 90 | tprints("NULL"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 91 | return; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 92 | } |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 93 | |
| 94 | if (!h || !verbose(tcp) || |
| 95 | (exiting(tcp) && syserror(tcp))) { |
| 96 | tprintf("%#lx", addr); |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | if (_LINUX_CAPABILITY_VERSION_2 == h->version || |
| 101 | _LINUX_CAPABILITY_VERSION_3 == h->version) |
| 102 | len = 2; |
| 103 | else |
| 104 | len = 1; |
| 105 | |
| 106 | if (umoven(tcp, addr, len * sizeof(data[0]), (char *) data) < 0) { |
| 107 | tprintf("%#lx", addr); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | tprints("{"); |
| 112 | print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0); |
| 113 | tprints(", "); |
| 114 | print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0); |
| 115 | tprints(", "); |
| 116 | print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0); |
| 117 | tprints("}"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | int |
| 121 | sys_capget(struct tcb *tcp) |
| 122 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 123 | cap_user_header_t h; |
| 124 | |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 125 | if (entering(tcp)) { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 126 | h = get_cap_header(tcp, tcp->u_arg[0]); |
| 127 | print_cap_header(tcp, tcp->u_arg[0], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 128 | tprints(", "); |
| 129 | } else { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 130 | h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]); |
| 131 | print_cap_data(tcp, tcp->u_arg[1], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int |
| 137 | sys_capset(struct tcb *tcp) |
| 138 | { |
| 139 | if (entering(tcp)) { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 140 | cap_user_header_t h = get_cap_header(tcp, tcp->u_arg[0]); |
| 141 | print_cap_header(tcp, tcp->u_arg[0], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 142 | tprints(", "); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 143 | print_cap_data(tcp, tcp->u_arg[1], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 144 | } |
| 145 | return 0; |
| 146 | } |