Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 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> |
Elliott Hughes | 39bac05 | 2017-05-25 16:56:11 -0700 | [diff] [blame] | 5 | * Copyright (c) 2014-2017 The strace developers. |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 31 | #include "defs.h" |
| 32 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 33 | /* these constants are the same as in <linux/capability.h> */ |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 34 | enum { |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 35 | #include "caps0.h" |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 36 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 37 | |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 38 | #include "xlat/cap_mask0.h" |
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 | /* these constants are CAP_TO_INDEX'ed constants from <linux/capability.h> */ |
| 41 | enum { |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 42 | #include "caps1.h" |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 45 | #include "xlat/cap_mask1.h" |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 46 | |
| 47 | /* these constants are the same as in <linux/capability.h> */ |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 48 | enum { |
| 49 | _LINUX_CAPABILITY_VERSION_1 = 0x19980330, |
| 50 | _LINUX_CAPABILITY_VERSION_2 = 0x20071026, |
| 51 | _LINUX_CAPABILITY_VERSION_3 = 0x20080522 |
| 52 | }; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 53 | |
| 54 | #include "xlat/cap_version.h" |
| 55 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 56 | struct user_cap_header_struct { |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 57 | uint32_t version; |
| 58 | int pid; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 59 | }; |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 60 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 61 | struct user_cap_data_struct { |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 62 | uint32_t effective; |
| 63 | uint32_t permitted; |
| 64 | uint32_t inheritable; |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 65 | }; |
Dmitry V. Levin | bf7fdfa | 2014-12-03 20:39:20 +0000 | [diff] [blame] | 66 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 67 | static const struct user_cap_header_struct * |
| 68 | get_cap_header(struct tcb *const tcp, const kernel_ulong_t addr) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 69 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 70 | static struct user_cap_header_struct header; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 71 | |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 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 |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 82 | print_cap_header(struct tcb *const tcp, const kernel_ulong_t addr, |
| 83 | const struct user_cap_header_struct *const h) |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 84 | { |
Dmitry V. Levin | c70da7c | 2015-07-20 17:50:56 +0000 | [diff] [blame] | 85 | if (!addr || !h) { |
| 86 | printaddr(addr); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 87 | return; |
| 88 | } |
| 89 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 90 | tprints("{version="); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 91 | printxval(cap_version, h->version, |
| 92 | "_LINUX_CAPABILITY_VERSION_???"); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 93 | tprintf(", pid=%d}", h->pid); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static void |
| 97 | print_cap_bits(const uint32_t lo, const uint32_t hi) |
| 98 | { |
| 99 | if (lo || !hi) |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 100 | printflags(cap_mask0, lo, "CAP_???"); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 101 | |
| 102 | if (hi) { |
| 103 | if (lo) |
| 104 | tprints("|"); |
Dmitry V. Levin | 2f0808b | 2015-02-18 23:59:50 +0000 | [diff] [blame] | 105 | printflags(cap_mask1, hi, "CAP_???"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | static void |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 110 | print_cap_data(struct tcb *const tcp, const kernel_ulong_t addr, |
| 111 | const struct user_cap_header_struct *const h) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 112 | { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 113 | struct user_cap_data_struct data[2]; |
| 114 | unsigned int len; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 115 | |
Dmitry V. Levin | c70da7c | 2015-07-20 17:50:56 +0000 | [diff] [blame] | 116 | if (!addr || !h) { |
| 117 | printaddr(addr); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 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 | |
Dmitry V. Levin | c70da7c | 2015-07-20 17:50:56 +0000 | [diff] [blame] | 127 | if (umoven_or_printaddr(tcp, addr, len * sizeof(data[0]), data)) |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 128 | return; |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 129 | |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 130 | tprints("{effective="); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 131 | print_cap_bits(data[0].effective, len > 1 ? data[1].effective : 0); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 132 | tprints(", permitted="); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 133 | print_cap_bits(data[0].permitted, len > 1 ? data[1].permitted : 0); |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 134 | tprints(", inheritable="); |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 135 | print_cap_bits(data[0].inheritable, len > 1 ? data[1].inheritable : 0); |
| 136 | tprints("}"); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Dmitry V. Levin | a0bd374 | 2015-04-07 01:36:50 +0000 | [diff] [blame] | 139 | SYS_FUNC(capget) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 140 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 141 | const struct user_cap_header_struct *h; |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 142 | |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 143 | if (entering(tcp)) { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 144 | h = get_cap_header(tcp, tcp->u_arg[0]); |
| 145 | print_cap_header(tcp, tcp->u_arg[0], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 146 | tprints(", "); |
| 147 | } else { |
Dmitry V. Levin | 4b9c68b | 2014-12-05 00:21:23 +0000 | [diff] [blame] | 148 | h = syserror(tcp) ? NULL : get_cap_header(tcp, tcp->u_arg[0]); |
| 149 | print_cap_data(tcp, tcp->u_arg[1], h); |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
Dmitry V. Levin | a0bd374 | 2015-04-07 01:36:50 +0000 | [diff] [blame] | 154 | SYS_FUNC(capset) |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 155 | { |
Elliott Hughes | d35df49 | 2017-02-15 15:19:05 -0800 | [diff] [blame] | 156 | const struct user_cap_header_struct *const h = |
| 157 | get_cap_header(tcp, tcp->u_arg[0]); |
Dmitry V. Levin | ff33aac | 2015-07-20 17:54:02 +0000 | [diff] [blame] | 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; |
Dmitry V. Levin | 5e7987b | 2014-12-03 20:30:15 +0000 | [diff] [blame] | 163 | } |