]> granicus.if.org Git - strace/blob - capability.c
tests: move F_OFD_SETLK* checks from fcntl64.c to fcntl-common.c
[strace] / capability.c
1 /*
2  * Copyright (c) 2000 Wichert Akkerman <wakkerma@debian.org>
3  * Copyright (c) 2011 Denys Vlasenko <dvlasenk@redhat.com>
4  * Copyright (c) 2005-2015 Dmitry V. Levin <ldv@altlinux.org>
5  * Copyright (c) 2014-2017 The strace developers.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "defs.h"
32
33 /* these constants are the same as in <linux/capability.h> */
34 enum {
35 #include "caps0.h"
36 };
37
38 #include "xlat/cap_mask0.h"
39
40 /* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */
41 enum {
42 #include "caps1.h"
43 };
44
45 #include "xlat/cap_mask1.h"
46
47 /* these constants are the same as in <linux/capability.h> */
48 enum {
49         _LINUX_CAPABILITY_VERSION_1 = 0x19980330,
50         _LINUX_CAPABILITY_VERSION_2 = 0x20071026,
51         _LINUX_CAPABILITY_VERSION_3 = 0x20080522
52 };
53
54 #include "xlat/cap_version.h"
55
56 struct user_cap_header_struct {
57         uint32_t version;
58         int pid;
59 };
60
61 struct user_cap_data_struct {
62         uint32_t effective;
63         uint32_t permitted;
64         uint32_t inheritable;
65 };
66
67 static const struct user_cap_header_struct *
68 get_cap_header(struct tcb *const tcp, const kernel_ulong_t addr)
69 {
70         static struct user_cap_header_struct header;
71
72         if (!addr || !verbose(tcp))
73                 return NULL;
74
75         if (umove(tcp, addr, &header) < 0)
76                 return NULL;
77
78         return &header;
79 }
80
81 static void
82 print_cap_header(struct tcb *const tcp, const kernel_ulong_t addr,
83                  const struct user_cap_header_struct *const h)
84 {
85         if (!addr || !h) {
86                 printaddr(addr);
87                 return;
88         }
89
90         tprints("{version=");
91         printxval(cap_version, h->version,
92                   "_LINUX_CAPABILITY_VERSION_???");
93         tprintf(", pid=%d}", h->pid);
94 }
95
96 static void
97 print_cap_bits(const uint32_t lo, const uint32_t hi)
98 {
99         if (lo || !hi)
100                 printflags(cap_mask0, lo, "CAP_???");
101
102         if (hi) {
103                 if (lo)
104                         tprints("|");
105                 printflags(cap_mask1, hi, "CAP_???");
106         }
107 }
108
109 static void
110 print_cap_data(struct tcb *const tcp, const kernel_ulong_t addr,
111                const struct user_cap_header_struct *const h)
112 {
113         struct user_cap_data_struct data[2];
114         unsigned int len;
115
116         if (!addr || !h) {
117                 printaddr(addr);
118                 return;
119         }
120
121         if (_LINUX_CAPABILITY_VERSION_2 == h->version ||
122             _LINUX_CAPABILITY_VERSION_3 == h->version)
123                 len = 2;
124         else
125                 len = 1;
126
127         if (umoven_or_printaddr(tcp, addr, len * sizeof(data[0]), data))
128                 return;
129
130         tprints("{effective=");
131         print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0);
132         tprints(", permitted=");
133         print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0);
134         tprints(", inheritable=");
135         print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0);
136         tprints("}");
137 }
138
139 SYS_FUNC(capget)
140 {
141         const struct user_cap_header_struct *h;
142
143         if (entering(tcp)) {
144                 h = get_cap_header(tcp, tcp->u_arg[0]);
145                 print_cap_header(tcp, tcp->u_arg[0], h);
146                 tprints(", ");
147         } else {
148                 h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]);
149                 print_cap_data(tcp, tcp->u_arg[1], h);
150         }
151         return 0;
152 }
153
154 SYS_FUNC(capset)
155 {
156         const struct user_cap_header_struct *const h =
157                 get_cap_header(tcp, tcp->u_arg[0]);
158         print_cap_header(tcp, tcp->u_arg[0], h);
159         tprints(", ");
160         print_cap_data(tcp, tcp->u_arg[1], h);
161
162         return RVAL_DECODED;
163 }