blob: af4dffeb0b98e89e446b4560810736e9c8f7fb49 [file] [log] [blame]
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +00001#include "defs.h"
2
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +00003/* these constants are the same as in <linux/capability.h> */
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +00004enum {
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. Levin5e7987b2014-12-03 20:30:15 +000038
39#include "xlat/capabilities.h"
40
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000041/* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */
42enum {
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. Levinbf7fdfa2014-12-03 20:39:20 +000054enum {
55 _LINUX_CAPABILITY_VERSION_1 = 0x19980330,
56 _LINUX_CAPABILITY_VERSION_2 = 0x20071026,
57 _LINUX_CAPABILITY_VERSION_3 = 0x20080522
58};
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000059
60#include "xlat/cap_version.h"
61
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000062typedef struct user_cap_header_struct {
63 uint32_t version;
64 int pid;
65} *cap_user_header_t;
66
67typedef 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. Levin4b9c68b2014-12-05 00:21:23 +000073static cap_user_header_t
74get_cap_header(struct tcb *tcp, unsigned long addr)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000075{
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000076 static struct user_cap_header_struct header;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000077
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000078 if (!addr || !verbose(tcp))
79 return NULL;
80
81 if (umove(tcp, addr, &header) < 0)
82 return NULL;
83
84 return &header;
85}
86
87static void
88print_cap_header(struct tcb *tcp, unsigned long addr, cap_user_header_t h)
89{
90 if (!addr) {
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000091 tprints("NULL");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000092 return;
93 }
94
95 if (!h) {
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000096 tprintf("%#lx", addr);
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000097 return;
98 }
99
100 tprints("{");
101 printxval(cap_version, h->version,
102 "_LINUX_CAPABILITY_VERSION_???");
103 tprintf(", %d}", h->pid);
104}
105
106static void
107print_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. Levin5e7987b2014-12-03 20:30:15 +0000116 }
117}
118
119static void
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000120print_cap_data(struct tcb *tcp, unsigned long addr, const cap_user_header_t h)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000121{
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000122 struct user_cap_data_struct data[2];
123 unsigned int len;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000124
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000125 if (!addr) {
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000126 tprints("NULL");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000127 return;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000128 }
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000129
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. Levin5e7987b2014-12-03 20:30:15 +0000154}
155
156int
157sys_capget(struct tcb *tcp)
158{
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000159 cap_user_header_t h;
160
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000161 if (entering(tcp)) {
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000162 h = get_cap_header(tcp, tcp->u_arg[0]);
163 print_cap_header(tcp, tcp->u_arg[0], h);
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000164 tprints(", ");
165 } else {
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000166 h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]);
167 print_cap_data(tcp, tcp->u_arg[1], h);
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000168 }
169 return 0;
170}
171
172int
173sys_capset(struct tcb *tcp)
174{
175 if (entering(tcp)) {
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000176 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. Levin5e7987b2014-12-03 20:30:15 +0000178 tprints(", ");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000179 print_cap_data(tcp, tcp->u_arg[1], h);
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000180 }
181 return 0;
182}