blob: 760bddace49a268445d9a91592f91a01351e92d4 [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
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>
Elliott Hughes39bac052017-05-25 16:56:11 -07005 * Copyright (c) 2014-2017 The strace developers.
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00006 * 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
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000031#include "defs.h"
32
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000033/* these constants are the same as in <linux/capability.h> */
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000034enum {
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +000035#include "caps0.h"
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000036};
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000037
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +000038#include "xlat/cap_mask0.h"
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000039
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000040/* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */
41enum {
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +000042#include "caps1.h"
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000043};
44
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +000045#include "xlat/cap_mask1.h"
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000046
47/* these constants are the same as in <linux/capability.h> */
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000048enum {
49 _LINUX_CAPABILITY_VERSION_1 = 0x19980330,
50 _LINUX_CAPABILITY_VERSION_2 = 0x20071026,
51 _LINUX_CAPABILITY_VERSION_3 = 0x20080522
52};
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000053
54#include "xlat/cap_version.h"
55
Elliott Hughesd35df492017-02-15 15:19:05 -080056struct user_cap_header_struct {
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000057 uint32_t version;
58 int pid;
Elliott Hughesd35df492017-02-15 15:19:05 -080059};
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000060
Elliott Hughesd35df492017-02-15 15:19:05 -080061struct user_cap_data_struct {
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000062 uint32_t effective;
63 uint32_t permitted;
64 uint32_t inheritable;
Elliott Hughesd35df492017-02-15 15:19:05 -080065};
Dmitry V. Levinbf7fdfa2014-12-03 20:39:20 +000066
Elliott Hughesd35df492017-02-15 15:19:05 -080067static const struct user_cap_header_struct *
68get_cap_header(struct tcb *const tcp, const kernel_ulong_t addr)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000069{
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000070 static struct user_cap_header_struct header;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +000071
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000072 if (!addr || !verbose(tcp))
73 return NULL;
74
75 if (umove(tcp, addr, &header) < 0)
76 return NULL;
77
78 return &header;
79}
80
81static void
Elliott Hughesd35df492017-02-15 15:19:05 -080082print_cap_header(struct tcb *const tcp, const kernel_ulong_t addr,
83 const struct user_cap_header_struct *const h)
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000084{
Dmitry V. Levinc70da7c2015-07-20 17:50:56 +000085 if (!addr || !h) {
86 printaddr(addr);
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000087 return;
88 }
89
Elliott Hughesd35df492017-02-15 15:19:05 -080090 tprints("{version=");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000091 printxval(cap_version, h->version,
92 "_LINUX_CAPABILITY_VERSION_???");
Elliott Hughesd35df492017-02-15 15:19:05 -080093 tprintf(", pid=%d}", h->pid);
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +000094}
95
96static void
97print_cap_bits(const uint32_t lo, const uint32_t hi)
98{
99 if (lo || !hi)
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +0000100 printflags(cap_mask0, lo, "CAP_???");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000101
102 if (hi) {
103 if (lo)
104 tprints("|");
Dmitry V. Levin2f0808b2015-02-18 23:59:50 +0000105 printflags(cap_mask1, hi, "CAP_???");
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000106 }
107}
108
109static void
Elliott Hughesd35df492017-02-15 15:19:05 -0800110print_cap_data(struct tcb *const tcp, const kernel_ulong_t addr,
111 const struct user_cap_header_struct *const h)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000112{
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000113 struct user_cap_data_struct data[2];
114 unsigned int len;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000115
Dmitry V. Levinc70da7c2015-07-20 17:50:56 +0000116 if (!addr || !h) {
117 printaddr(addr);
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000118 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
Dmitry V. Levinc70da7c2015-07-20 17:50:56 +0000127 if (umoven_or_printaddr(tcp, addr, len * sizeof(data[0]), data))
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000128 return;
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000129
Elliott Hughesd35df492017-02-15 15:19:05 -0800130 tprints("{effective=");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000131 print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0);
Elliott Hughesd35df492017-02-15 15:19:05 -0800132 tprints(", permitted=");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000133 print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0);
Elliott Hughesd35df492017-02-15 15:19:05 -0800134 tprints(", inheritable=");
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000135 print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0);
136 tprints("}");
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000137}
138
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000139SYS_FUNC(capget)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000140{
Elliott Hughesd35df492017-02-15 15:19:05 -0800141 const struct user_cap_header_struct *h;
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000142
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000143 if (entering(tcp)) {
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000144 h = get_cap_header(tcp, tcp->u_arg[0]);
145 print_cap_header(tcp, tcp->u_arg[0], h);
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000146 tprints(", ");
147 } else {
Dmitry V. Levin4b9c68b2014-12-05 00:21:23 +0000148 h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]);
149 print_cap_data(tcp, tcp->u_arg[1], h);
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000150 }
151 return 0;
152}
153
Dmitry V. Levina0bd3742015-04-07 01:36:50 +0000154SYS_FUNC(capset)
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000155{
Elliott Hughesd35df492017-02-15 15:19:05 -0800156 const struct user_cap_header_struct *const h =
157 get_cap_header(tcp, tcp->u_arg[0]);
Dmitry V. Levinff33aac2015-07-20 17:54:02 +0000158 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;
Dmitry V. Levin5e7987b2014-12-03 20:30:15 +0000163}