Branch data Line data Source code
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 : 48 : get_cap_header(struct tcb *const tcp, const kernel_ulong_t addr)
69 : : {
70 : : static struct user_cap_header_struct header;
71 : :
72 [ + + ][ + + ]: 48 : if (!addr || !verbose(tcp))
73 : : return NULL;
74 : :
75 [ + + ]: 20 : if (umove(tcp, addr, &header) < 0)
76 : : return NULL;
77 : :
78 : 16 : return &header;
79 : : }
80 : :
81 : : static void
82 : 88 : print_cap_header(struct tcb *const tcp, const kernel_ulong_t addr,
83 : : const struct user_cap_header_struct *const h)
84 : : {
85 [ + + ]: 44 : if (!addr || !h) {
86 : 30 : printaddr(addr);
87 : 44 : return;
88 : : }
89 : :
90 : 14 : tprints("{version=");
91 : 14 : printxval(cap_version, h->version,
92 : : "_LINUX_CAPABILITY_VERSION_???");
93 : 14 : tprintf(", pid=%d}", h->pid);
94 : : }
95 : :
96 : : static void
97 : 24 : print_cap_bits(const uint32_t lo, const uint32_t hi)
98 : : {
99 [ + + ]: 24 : if (lo || !hi)
100 : : printflags(cap_mask0, lo, "CAP_???");
101 : :
102 [ + + ]: 24 : if (hi) {
103 [ + + ]: 8 : if (lo)
104 : 4 : tprints("|");
105 : : printflags(cap_mask1, hi, "CAP_???");
106 : : }
107 : 24 : }
108 : :
109 : : static void
110 : 44 : 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 [ + + ]: 44 : if (!addr || !h) {
117 : 32 : printaddr(addr);
118 : 36 : return;
119 : : }
120 : :
121 [ + + ]: 12 : if (_LINUX_CAPABILITY_VERSION_2 == h->version ||
122 : : _LINUX_CAPABILITY_VERSION_3 == h->version)
123 : : len = 2;
124 : : else
125 : 4 : len = 1;
126 : :
127 [ + + ]: 12 : if (umoven_or_printaddr(tcp, addr, len * sizeof(data[0]), data))
128 : : return;
129 : :
130 : 8 : tprints("{effective=");
131 [ + + ]: 8 : print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0);
132 : 8 : tprints(", permitted=");
133 [ + + ]: 8 : print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0);
134 : 8 : tprints(", inheritable=");
135 [ + + ]: 8 : print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0);
136 : 8 : tprints("}");
137 : : }
138 : :
139 : 32 : SYS_FUNC(capget)
140 : : {
141 : : const struct user_cap_header_struct *h;
142 : :
143 [ + + ]: 32 : if (entering(tcp)) {
144 : 16 : h = get_cap_header(tcp, tcp->u_arg[0]);
145 : 16 : print_cap_header(tcp, tcp->u_arg[0], h);
146 : 16 : tprints(", ");
147 : : } else {
148 [ + + ]: 16 : h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]);
149 : 16 : print_cap_data(tcp, tcp->u_arg[1], h);
150 : : }
151 : 32 : return 0;
152 : : }
153 : :
154 : 28 : SYS_FUNC(capset)
155 : : {
156 : 28 : const struct user_cap_header_struct *const h =
157 : 28 : get_cap_header(tcp, tcp->u_arg[0]);
158 : 28 : print_cap_header(tcp, tcp->u_arg[0], h);
159 : 28 : tprints(", ");
160 : 28 : print_cap_data(tcp, tcp->u_arg[1], h);
161 : :
162 : 28 : return RVAL_DECODED;
163 : : }
|