1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/*
* captest.c - A program that demonstrates and outputs capabilities
* Copyright (c) 2009, 2013, 2020 Red Hat Inc.
* All Rights Reserved.
*
* This software may be freely redistributed and/or modified under the
* terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1335, USA.
*
* Authors:
* Steve Grubb <sgrubb@redhat.com>
*
*/
#include "config.h"
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <cap-ng.h>
#include <sys/prctl.h>
#ifdef HAVE_LINUX_SECUREBITS_H
#include <linux/securebits.h>
#endif
/* children can't get caps back */
#ifndef SECURE_NOROOT
#define SECURE_NOROOT 0
#endif
#ifndef SECURE_NOROOT_LOCKED
#define SECURE_NOROOT_LOCKED 1 /* make bit-0 immutable */
#endif
/* Setuid apps run by uid 0 don't get caps back */
#ifndef SECURE_NO_SETUID_FIXUP
#define SECURE_NO_SETUID_FIXUP 2
#endif
#ifndef SECURE_NO_SETUID_FIXUP_LOCKED
#define SECURE_NO_SETUID_FIXUP_LOCKED 3 /* make bit-2 immutable */
#endif
static int text = 0, no_child = 0, lock = 0, ambient = 0;
static void report(void)
{
int rc, escalated = 0, need_comma = 0;
uid_t uid, euid, suid;
gid_t gid, egid, sgid;
// Refresh what we have for capabilities
if (capng_get_caps_process()) {
printf("Error getting capabilities\n");
exit(1);
}
// Check user credentials
getresuid(&uid, &euid, &suid);
getresgid(&gid, &egid, &sgid);
if (no_child) {
if ((uid != euid && uid != 0) ||
capng_have_capability(CAPNG_EFFECTIVE,
CAP_SETUID)) {
printf("Attempting to regain root...");
setuid(0);
getresuid(&uid, &euid, &suid);
if (uid == 0) {
printf("SUCCESS - PRIVILEGE ESCALATION POSSIBLE\n");
setgid(0);
getresgid(&gid, &egid, &sgid);
escalated = 1;
} else
printf("FAILED\n");
}
printf("Child ");
}
printf("User credentials uid:%u euid:%u suid:%u\n", uid, euid, suid);
if (no_child)
printf("Child ");
printf("Group credentials gid:%u egid:%u sgid:%u\n", gid, egid, sgid);
if (uid != euid || gid != egid)
printf("Note: app has mismatching credentials!!\n");
// Check capabilities
if (text) {
if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) {
if (no_child)
printf("Child capabilities: none\n");
else
printf("Current capabilities: none\n");
} else {
if (no_child)
printf("Child ");
printf("Effective: ");
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_EFFECTIVE);
printf("\n");
if (no_child)
printf("Child ");
printf("Permitted: ");
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_PERMITTED);
printf("\n");
if (no_child)
printf("Child ");
printf("Inheritable: ");
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_INHERITABLE);
printf("\n");
if (no_child)
printf("Child ");
printf("Bounding Set: ");
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_BOUNDING_SET);
printf("\n");
if (no_child)
printf("Child ");
printf("Ambient: ");
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_AMBIENT);
printf("\n");
}
} else {
if (capng_have_capabilities(CAPNG_SELECT_CAPS) == CAPNG_NONE) {
if (no_child)
printf("Child capabilities: none\n");
else
printf("Current capabilities: none\n");
} else {
if (no_child)
printf("Child capabilities:\n");
capng_print_caps_numeric(CAPNG_PRINT_STDOUT,
CAPNG_SELECT_ALL);
}
}
// Now check securebits flags
#ifdef PR_SET_SECUREBITS
if (no_child)
printf("Child ");
printf("securebits flags: ");
rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NOROOT);
if (rc & (1 << SECURE_NOROOT)) {
printf("NOROOT");
need_comma = 1;
}
rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NOROOT_LOCKED);
if (rc & (1 << SECURE_NOROOT_LOCKED)) {
if (need_comma)
printf(", ");
printf("NOROOT_LOCKED");
need_comma = 1;
}
rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NO_SETUID_FIXUP);
if (rc & (1 << SECURE_NO_SETUID_FIXUP)) {
if (need_comma)
printf(", ");
printf("NO_SETUID_FIXUP");
need_comma = 1;
}
rc = prctl(PR_GET_SECUREBITS, 1 << SECURE_NO_SETUID_FIXUP_LOCKED);
if (rc & (1 << SECURE_NO_SETUID_FIXUP_LOCKED)) {
if (need_comma)
printf(", ");
printf("NO_SETUID_FIXUP_LOCKED");
need_comma = 1;
}
if (need_comma == 0)
printf("none");
printf("\n");
#endif
// Now do child process checks
if (no_child == 0 || escalated) {
printf("Attempting direct access to shadow...");
if (access("/etc/shadow", R_OK) == 0)
printf("SUCCESS\n");
else
printf("FAILED (%s)\n", strerror(errno));
}
if (no_child == 0) {
printf("Attempting to access shadow by child process...");
rc = system("cat /etc/shadow > /dev/null 2>&1");
if (rc == 0)
printf("SUCCESS\n");
else
printf("FAILED\n");
if (text)
system("/usr/bin/captest --no-child --text");
else
system("/usr/bin/captest --no-child");
}
}
static void usage(void)
{
printf("usage: captest [ --ambient | --drop-all | --drop-caps | --id | --init-grp ] [ --lock ] [ --text ]\n");
}
int main(int argc, char *argv[])
{
int which = 0, i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--text") == 0)
text = 1;
else if (strcmp(argv[i], "--no-child") == 0)
no_child = 1;
else if (strcmp(argv[i], "--lock") == 0)
lock = 1;
else if (strcmp(argv[i], "--drop-all") == 0)
which = 1;
else if (strcmp(argv[i], "--drop-caps") == 0)
which = 2;
else if (strcmp(argv[i], "--id") == 0)
which = 3;
else if (strcmp(argv[i], "--init-grp") == 0)
which = 4;
else if (strcmp(argv[i], "--ambient") == 0)
ambient = 1;
else {
usage();
return 0;
}
}
switch (which)
{
case 1:
capng_clear(CAPNG_SELECT_ALL);
if (lock) {
if (capng_lock()) {
fprintf(stderr, "capng_lock failed\n");
return 1;
}
}
if (capng_apply(CAPNG_SELECT_ALL)) {
fprintf(stderr, "capng_apply failed\n");
return 1;
}
report();
break;
case 2:
capng_clear(CAPNG_SELECT_CAPS);
if (ambient)
capng_update(CAPNG_ADD, CAPNG_AMBIENT,
CAP_CHOWN);
if (lock) {
if (capng_lock()) {
fprintf(stderr, "capng_lock failed\n");
return 1;
}
}
if (ambient) {
if (capng_apply(
CAPNG_SELECT_CAPS|CAPNG_SELECT_AMBIENT)) {
fprintf(stderr, "capng_apply failed\n");
return 1;
}
} else {
if (capng_apply(CAPNG_SELECT_CAPS)) {
fprintf(stderr, "capng_apply failed\n");
return 1;
}
}
report();
break;
case 3:
case 4: {
int rc;
capng_clear(CAPNG_SELECT_BOTH);
capng_update(CAPNG_ADD, CAPNG_EFFECTIVE|CAPNG_PERMITTED,
CAP_CHOWN);
if (ambient)
capng_update(CAPNG_ADD,
CAPNG_INHERITABLE|CAPNG_AMBIENT,
CAP_CHOWN);
if (which == 4)
rc = capng_change_id(99, 99,
CAPNG_INIT_SUPP_GRP | CAPNG_CLEAR_BOUNDING);
else
rc = capng_change_id(99, 99,
CAPNG_DROP_SUPP_GRP | CAPNG_CLEAR_BOUNDING);
if (rc < 0) {
printf("Error changing uid: %d\n", rc);
capng_print_caps_text(CAPNG_PRINT_STDOUT,
CAPNG_EFFECTIVE);
printf("\n");
return 1;
}
printf("Keeping CAP_CHOWN to show capabilities across uid change.\n");
report();
} break;
case 0:
if (lock) {
if (capng_lock()) {
fprintf(stderr, "capng_lock failed\n");
return 1;
}
}
report();
break;
}
return 0;
}
|