1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/kernel/capability.c 4 * 5 * Copyright (C) 1997 Andrew Main <zefram@fysh.org> 6 * 7 * Integrated into 2.1.97+, Andrew G. Morgan <morgan@kernel.org> 8 * 30 May 2002: Cleanup, Robert M. Love <rml@tech9.net> 9 */ 10 11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 12 13 #include <linux/audit.h> 14 #include <linux/capability.h> 15 #include <linux/mm.h> 16 #include <linux/export.h> 17 #include <linux/security.h> 18 #include <linux/syscalls.h> 19 #include <linux/pid_namespace.h> 20 #include <linux/user_namespace.h> 21 #include <linux/uaccess.h> 22 23 int file_caps_enabled = 1; 24 25 static int __init file_caps_disable(char *str) 26 { 27 file_caps_enabled = 0; 28 return 1; 29 } 30 __setup("no_file_caps", file_caps_disable); 31 32 #ifdef CONFIG_MULTIUSER 33 /* 34 * More recent versions of libcap are available from: 35 * 36 * http://www.kernel.org/pub/linux/libs/security/linux-privs/ 37 */ 38 39 static void warn_legacy_capability_use(void) 40 { 41 pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n", 42 current->comm); 43 } 44 45 /* 46 * Version 2 capabilities worked fine, but the linux/capability.h file 47 * that accompanied their introduction encouraged their use without 48 * the necessary user-space source code changes. As such, we have 49 * created a version 3 with equivalent functionality to version 2, but 50 * with a header change to protect legacy source code from using 51 * version 2 when it wanted to use version 1. If your system has code 52 * that trips the following warning, it is using version 2 specific 53 * capabilities and may be doing so insecurely. 54 * 55 * The remedy is to either upgrade your version of libcap (to 2.10+, 56 * if the application is linked against it), or recompile your 57 * application with modern kernel headers and this warning will go 58 * away. 59 */ 60 61 static void warn_deprecated_v2(void) 62 { 63 pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n", 64 current->comm); 65 } 66 67 /* 68 * Version check. Return the number of u32s in each capability flag 69 * array, or a negative value on error. 70 */ 71 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy) 72 { 73 __u32 version; 74 75 if (get_user(version, &header->version)) 76 return -EFAULT; 77 78 switch (version) { 79 case _LINUX_CAPABILITY_VERSION_1: 80 warn_legacy_capability_use(); 81 *tocopy = _LINUX_CAPABILITY_U32S_1; 82 break; 83 case _LINUX_CAPABILITY_VERSION_2: 84 warn_deprecated_v2(); 85 fallthrough; /* v3 is otherwise equivalent to v2 */ 86 case _LINUX_CAPABILITY_VERSION_3: 87 *tocopy = _LINUX_CAPABILITY_U32S_3; 88 break; 89 default: 90 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version)) 91 return -EFAULT; 92 return -EINVAL; 93 } 94 95 return 0; 96 } 97 98 /* 99 * The only thing that can change the capabilities of the current 100 * process is the current process. As such, we can't be in this code 101 * at the same time as we are in the process of setting capabilities 102 * in this process. The net result is that we can limit our use of 103 * locks to when we are reading the caps of another process. 104 */ 105 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp, 106 kernel_cap_t *pIp, kernel_cap_t *pPp) 107 { 108 int ret; 109 110 if (pid && (pid != task_pid_vnr(current))) { 111 const struct task_struct *target; 112 113 rcu_read_lock(); 114 115 target = find_task_by_vpid(pid); 116 if (!target) 117 ret = -ESRCH; 118 else 119 ret = security_capget(target, pEp, pIp, pPp); 120 121 rcu_read_unlock(); 122 } else 123 ret = security_capget(current, pEp, pIp, pPp); 124 125 return ret; 126 } 127 128 /** 129 * sys_capget - get the capabilities of a given process. 130 * @header: pointer to struct that contains capability version and 131 * target pid data 132 * @dataptr: pointer to struct that contains the effective, permitted, 133 * and inheritable capabilities that are returned 134 * 135 * Returns 0 on success and < 0 on error. 136 */ 137 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr) 138 { 139 int ret = 0; 140 pid_t pid; 141 unsigned tocopy; 142 kernel_cap_t pE, pI, pP; 143 struct __user_cap_data_struct kdata[2]; 144 145 ret = cap_validate_magic(header, &tocopy); 146 if ((dataptr == NULL) || (ret != 0)) 147 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret; 148 149 if (get_user(pid, &header->pid)) 150 return -EFAULT; 151 152 if (pid < 0) 153 return -EINVAL; 154 155 ret = cap_get_target_pid(pid, &pE, &pI, &pP); 156 if (ret) 157 return ret; 158 159 /* 160 * Annoying legacy format with 64-bit capabilities exposed 161 * as two sets of 32-bit fields, so we need to split the 162 * capability values up. 163 */ 164 kdata[0].effective = pE.val; kdata[1].effective = pE.val >> 32; 165 kdata[0].permitted = pP.val; kdata[1].permitted = pP.val >> 32; 166 kdata[0].inheritable = pI.val; kdata[1].inheritable = pI.val >> 32; 167 168 /* 169 * Note, in the case, tocopy < _KERNEL_CAPABILITY_U32S, 170 * we silently drop the upper capabilities here. This 171 * has the effect of making older libcap 172 * implementations implicitly drop upper capability 173 * bits when they perform a: capget/modify/capset 174 * sequence. 175 * 176 * This behavior is considered fail-safe 177 * behavior. Upgrading the application to a newer 178 * version of libcap will enable access to the newer 179 * capabilities. 180 * 181 * An alternative would be to return an error here 182 * (-ERANGE), but that causes legacy applications to 183 * unexpectedly fail; the capget/modify/capset aborts 184 * before modification is attempted and the application 185 * fails. 186 */ 187 if (copy_to_user(dataptr, kdata, tocopy * sizeof(kdata[0]))) 188 return -EFAULT; 189 190 return 0; 191 } 192 193 static kernel_cap_t mk_kernel_cap(u32 low, u32 high) 194 { 195 return (kernel_cap_t) { (low | ((u64)high << 32)) & CAP_VALID_MASK }; 196 } 197 198 /** 199 * sys_capset - set capabilities for a process or (*) a group of processes 200 * @header: pointer to struct that contains capability version and 201 * target pid data 202 * @data: pointer to struct that contains the effective, permitted, 203 * and inheritable capabilities 204 * 205 * Set capabilities for the current process only. The ability to any other 206 * process(es) has been deprecated and removed. 207 * 208 * The restrictions on setting capabilities are specified as: 209 * 210 * I: any raised capabilities must be a subset of the old permitted 211 * P: any raised capabilities must be a subset of the old permitted 212 * E: must be set to a subset of new permitted 213 * 214 * Returns 0 on success and < 0 on error. 215 */ 216 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data) 217 { 218 struct __user_cap_data_struct kdata[2] = { { 0, }, }; 219 unsigned tocopy, copybytes; 220 kernel_cap_t inheritable, permitted, effective; 221 struct cred *new; 222 int ret; 223 pid_t pid; 224 225 ret = cap_validate_magic(header, &tocopy); 226 if (ret != 0) 227 return ret; 228 229 if (get_user(pid, &header->pid)) 230 return -EFAULT; 231 232 /* may only affect current now */ 233 if (pid != 0 && pid != task_pid_vnr(current)) 234 return -EPERM; 235 236 copybytes = tocopy * sizeof(struct __user_cap_data_struct); 237 if (copybytes > sizeof(kdata)) 238 return -EFAULT; 239 240 if (copy_from_user(&kdata, data, copybytes)) 241 return -EFAULT; 242 243 effective = mk_kernel_cap(kdata[0].effective, kdata[1].effective); 244 permitted = mk_kernel_cap(kdata[0].permitted, kdata[1].permitted); 245 inheritable = mk_kernel_cap(kdata[0].inheritable, kdata[1].inheritable); 246 247 new = prepare_creds(); 248 if (!new) 249 return -ENOMEM; 250 251 ret = security_capset(new, current_cred(), 252 &effective, &inheritable, &permitted); 253 if (ret < 0) 254 goto error; 255 256 audit_log_capset(new, current_cred()); 257 258 return commit_creds(new); 259 260 error: 261 abort_creds(new); 262 return ret; 263 } 264 265 /** 266 * has_ns_capability - Does a task have a capability in a specific user ns 267 * @t: The task in question 268 * @ns: target user namespace 269 * @cap: The capability to be tested for 270 * 271 * Return true if the specified task has the given superior capability 272 * currently in effect to the specified user namespace, false if not. 273 * 274 * Note that this does not set PF_SUPERPRIV on the task. 275 */ 276 bool has_ns_capability(struct task_struct *t, 277 struct user_namespace *ns, int cap) 278 { 279 int ret; 280 281 rcu_read_lock(); 282 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE); 283 rcu_read_unlock(); 284 285 return (ret == 0); 286 } 287 288 /** 289 * has_capability - Does a task have a capability in init_user_ns 290 * @t: The task in question 291 * @cap: The capability to be tested for 292 * 293 * Return true if the specified task has the given superior capability 294 * currently in effect to the initial user namespace, false if not. 295 * 296 * Note that this does not set PF_SUPERPRIV on the task. 297 */ 298 bool has_capability(struct task_struct *t, int cap) 299 { 300 return has_ns_capability(t, &init_user_ns, cap); 301 } 302 EXPORT_SYMBOL(has_capability); 303 304 /** 305 * has_ns_capability_noaudit - Does a task have a capability (unaudited) 306 * in a specific user ns. 307 * @t: The task in question 308 * @ns: target user namespace 309 * @cap: The capability to be tested for 310 * 311 * Return true if the specified task has the given superior capability 312 * currently in effect to the specified user namespace, false if not. 313 * Do not write an audit message for the check. 314 * 315 * Note that this does not set PF_SUPERPRIV on the task. 316 */ 317 bool has_ns_capability_noaudit(struct task_struct *t, 318 struct user_namespace *ns, int cap) 319 { 320 int ret; 321 322 rcu_read_lock(); 323 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT); 324 rcu_read_unlock(); 325 326 return (ret == 0); 327 } 328 329 /** 330 * has_capability_noaudit - Does a task have a capability (unaudited) in the 331 * initial user ns 332 * @t: The task in question 333 * @cap: The capability to be tested for 334 * 335 * Return true if the specified task has the given superior capability 336 * currently in effect to init_user_ns, false if not. Don't write an 337 * audit message for the check. 338 * 339 * Note that this does not set PF_SUPERPRIV on the task. 340 */ 341 bool has_capability_noaudit(struct task_struct *t, int cap) 342 { 343 return has_ns_capability_noaudit(t, &init_user_ns, cap); 344 } 345 EXPORT_SYMBOL(has_capability_noaudit); 346 347 static bool ns_capable_common(struct user_namespace *ns, 348 int cap, 349 unsigned int opts) 350 { 351 int capable; 352 353 if (unlikely(!cap_valid(cap))) { 354 pr_crit("capable() called with invalid cap=%u\n", cap); 355 BUG(); 356 } 357 358 capable = security_capable(current_cred(), ns, cap, opts); 359 if (capable == 0) { 360 current->flags |= PF_SUPERPRIV; 361 return true; 362 } 363 return false; 364 } 365 366 /** 367 * ns_capable - Determine if the current task has a superior capability in effect 368 * @ns: The usernamespace we want the capability in 369 * @cap: The capability to be tested for 370 * 371 * Return true if the current task has the given superior capability currently 372 * available for use, false if not. 373 * 374 * This sets PF_SUPERPRIV on the task if the capability is available on the 375 * assumption that it's about to be used. 376 */ 377 bool ns_capable(struct user_namespace *ns, int cap) 378 { 379 return ns_capable_common(ns, cap, CAP_OPT_NONE); 380 } 381 EXPORT_SYMBOL(ns_capable); 382 383 /** 384 * ns_capable_noaudit - Determine if the current task has a superior capability 385 * (unaudited) in effect 386 * @ns: The usernamespace we want the capability in 387 * @cap: The capability to be tested for 388 * 389 * Return true if the current task has the given superior capability currently 390 * available for use, false if not. 391 * 392 * This sets PF_SUPERPRIV on the task if the capability is available on the 393 * assumption that it's about to be used. 394 */ 395 bool ns_capable_noaudit(struct user_namespace *ns, int cap) 396 { 397 return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT); 398 } 399 EXPORT_SYMBOL(ns_capable_noaudit); 400 401 /** 402 * ns_capable_setid - Determine if the current task has a superior capability 403 * in effect, while signalling that this check is being done from within a 404 * setid or setgroups syscall. 405 * @ns: The usernamespace we want the capability in 406 * @cap: The capability to be tested for 407 * 408 * Return true if the current task has the given superior capability currently 409 * available for use, false if not. 410 * 411 * This sets PF_SUPERPRIV on the task if the capability is available on the 412 * assumption that it's about to be used. 413 */ 414 bool ns_capable_setid(struct user_namespace *ns, int cap) 415 { 416 return ns_capable_common(ns, cap, CAP_OPT_INSETID); 417 } 418 EXPORT_SYMBOL(ns_capable_setid); 419 420 /** 421 * capable - Determine if the current task has a superior capability in effect 422 * @cap: The capability to be tested for 423 * 424 * Return true if the current task has the given superior capability currently 425 * available for use, false if not. 426 * 427 * This sets PF_SUPERPRIV on the task if the capability is available on the 428 * assumption that it's about to be used. 429 */ 430 bool capable(int cap) 431 { 432 return ns_capable(&init_user_ns, cap); 433 } 434 EXPORT_SYMBOL(capable); 435 #endif /* CONFIG_MULTIUSER */ 436 437 /** 438 * file_ns_capable - Determine if the file's opener had a capability in effect 439 * @file: The file we want to check 440 * @ns: The usernamespace we want the capability in 441 * @cap: The capability to be tested for 442 * 443 * Return true if task that opened the file had a capability in effect 444 * when the file was opened. 445 * 446 * This does not set PF_SUPERPRIV because the caller may not 447 * actually be privileged. 448 */ 449 bool file_ns_capable(const struct file *file, struct user_namespace *ns, 450 int cap) 451 { 452 453 if (WARN_ON_ONCE(!cap_valid(cap))) 454 return false; 455 456 if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0) 457 return true; 458 459 return false; 460 } 461 EXPORT_SYMBOL(file_ns_capable); 462 463 /** 464 * privileged_wrt_inode_uidgid - Do capabilities in the namespace work over the inode? 465 * @ns: The user namespace in question 466 * @idmap: idmap of the mount @inode was found from 467 * @inode: The inode in question 468 * 469 * Return true if the inode uid and gid are within the namespace. 470 */ 471 bool privileged_wrt_inode_uidgid(struct user_namespace *ns, 472 struct mnt_idmap *idmap, 473 const struct inode *inode) 474 { 475 return vfsuid_has_mapping(ns, i_uid_into_vfsuid(idmap, inode)) && 476 vfsgid_has_mapping(ns, i_gid_into_vfsgid(idmap, inode)); 477 } 478 479 /** 480 * capable_wrt_inode_uidgid - Check nsown_capable and uid and gid mapped 481 * @idmap: idmap of the mount @inode was found from 482 * @inode: The inode in question 483 * @cap: The capability in question 484 * 485 * Return true if the current task has the given capability targeted at 486 * its own user namespace and that the given inode's uid and gid are 487 * mapped into the current user namespace. 488 */ 489 bool capable_wrt_inode_uidgid(struct mnt_idmap *idmap, 490 const struct inode *inode, int cap) 491 { 492 struct user_namespace *ns = current_user_ns(); 493 494 return ns_capable(ns, cap) && 495 privileged_wrt_inode_uidgid(ns, idmap, inode); 496 } 497 EXPORT_SYMBOL(capable_wrt_inode_uidgid); 498 499 /** 500 * ptracer_capable - Determine if the ptracer holds CAP_SYS_PTRACE in the namespace 501 * @tsk: The task that may be ptraced 502 * @ns: The user namespace to search for CAP_SYS_PTRACE in 503 * 504 * Return true if the task that is ptracing the current task had CAP_SYS_PTRACE 505 * in the specified user namespace. 506 */ 507 bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns) 508 { 509 int ret = 0; /* An absent tracer adds no restrictions */ 510 const struct cred *cred; 511 512 rcu_read_lock(); 513 cred = rcu_dereference(tsk->ptracer_cred); 514 if (cred) 515 ret = security_capable(cred, ns, CAP_SYS_PTRACE, 516 CAP_OPT_NOAUDIT); 517 rcu_read_unlock(); 518 return (ret == 0); 519 } 520