0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0012
0013 #include <linux/audit.h>
0014 #include <linux/capability.h>
0015 #include <linux/mm.h>
0016 #include <linux/export.h>
0017 #include <linux/security.h>
0018 #include <linux/syscalls.h>
0019 #include <linux/pid_namespace.h>
0020 #include <linux/user_namespace.h>
0021 #include <linux/uaccess.h>
0022
0023
0024
0025
0026
0027 const kernel_cap_t __cap_empty_set = CAP_EMPTY_SET;
0028 EXPORT_SYMBOL(__cap_empty_set);
0029
0030 int file_caps_enabled = 1;
0031
0032 static int __init file_caps_disable(char *str)
0033 {
0034 file_caps_enabled = 0;
0035 return 1;
0036 }
0037 __setup("no_file_caps", file_caps_disable);
0038
0039 #ifdef CONFIG_MULTIUSER
0040
0041
0042
0043
0044
0045
0046 static void warn_legacy_capability_use(void)
0047 {
0048 char name[sizeof(current->comm)];
0049
0050 pr_info_once("warning: `%s' uses 32-bit capabilities (legacy support in use)\n",
0051 get_task_comm(name, current));
0052 }
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070 static void warn_deprecated_v2(void)
0071 {
0072 char name[sizeof(current->comm)];
0073
0074 pr_info_once("warning: `%s' uses deprecated v2 capabilities in a way that may be insecure\n",
0075 get_task_comm(name, current));
0076 }
0077
0078
0079
0080
0081
0082 static int cap_validate_magic(cap_user_header_t header, unsigned *tocopy)
0083 {
0084 __u32 version;
0085
0086 if (get_user(version, &header->version))
0087 return -EFAULT;
0088
0089 switch (version) {
0090 case _LINUX_CAPABILITY_VERSION_1:
0091 warn_legacy_capability_use();
0092 *tocopy = _LINUX_CAPABILITY_U32S_1;
0093 break;
0094 case _LINUX_CAPABILITY_VERSION_2:
0095 warn_deprecated_v2();
0096 fallthrough;
0097 case _LINUX_CAPABILITY_VERSION_3:
0098 *tocopy = _LINUX_CAPABILITY_U32S_3;
0099 break;
0100 default:
0101 if (put_user((u32)_KERNEL_CAPABILITY_VERSION, &header->version))
0102 return -EFAULT;
0103 return -EINVAL;
0104 }
0105
0106 return 0;
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116 static inline int cap_get_target_pid(pid_t pid, kernel_cap_t *pEp,
0117 kernel_cap_t *pIp, kernel_cap_t *pPp)
0118 {
0119 int ret;
0120
0121 if (pid && (pid != task_pid_vnr(current))) {
0122 struct task_struct *target;
0123
0124 rcu_read_lock();
0125
0126 target = find_task_by_vpid(pid);
0127 if (!target)
0128 ret = -ESRCH;
0129 else
0130 ret = security_capget(target, pEp, pIp, pPp);
0131
0132 rcu_read_unlock();
0133 } else
0134 ret = security_capget(current, pEp, pIp, pPp);
0135
0136 return ret;
0137 }
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 SYSCALL_DEFINE2(capget, cap_user_header_t, header, cap_user_data_t, dataptr)
0149 {
0150 int ret = 0;
0151 pid_t pid;
0152 unsigned tocopy;
0153 kernel_cap_t pE, pI, pP;
0154
0155 ret = cap_validate_magic(header, &tocopy);
0156 if ((dataptr == NULL) || (ret != 0))
0157 return ((dataptr == NULL) && (ret == -EINVAL)) ? 0 : ret;
0158
0159 if (get_user(pid, &header->pid))
0160 return -EFAULT;
0161
0162 if (pid < 0)
0163 return -EINVAL;
0164
0165 ret = cap_get_target_pid(pid, &pE, &pI, &pP);
0166 if (!ret) {
0167 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
0168 unsigned i;
0169
0170 for (i = 0; i < tocopy; i++) {
0171 kdata[i].effective = pE.cap[i];
0172 kdata[i].permitted = pP.cap[i];
0173 kdata[i].inheritable = pI.cap[i];
0174 }
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195 if (copy_to_user(dataptr, kdata, tocopy
0196 * sizeof(struct __user_cap_data_struct))) {
0197 return -EFAULT;
0198 }
0199 }
0200
0201 return ret;
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222 SYSCALL_DEFINE2(capset, cap_user_header_t, header, const cap_user_data_t, data)
0223 {
0224 struct __user_cap_data_struct kdata[_KERNEL_CAPABILITY_U32S];
0225 unsigned i, tocopy, copybytes;
0226 kernel_cap_t inheritable, permitted, effective;
0227 struct cred *new;
0228 int ret;
0229 pid_t pid;
0230
0231 ret = cap_validate_magic(header, &tocopy);
0232 if (ret != 0)
0233 return ret;
0234
0235 if (get_user(pid, &header->pid))
0236 return -EFAULT;
0237
0238
0239 if (pid != 0 && pid != task_pid_vnr(current))
0240 return -EPERM;
0241
0242 copybytes = tocopy * sizeof(struct __user_cap_data_struct);
0243 if (copybytes > sizeof(kdata))
0244 return -EFAULT;
0245
0246 if (copy_from_user(&kdata, data, copybytes))
0247 return -EFAULT;
0248
0249 for (i = 0; i < tocopy; i++) {
0250 effective.cap[i] = kdata[i].effective;
0251 permitted.cap[i] = kdata[i].permitted;
0252 inheritable.cap[i] = kdata[i].inheritable;
0253 }
0254 while (i < _KERNEL_CAPABILITY_U32S) {
0255 effective.cap[i] = 0;
0256 permitted.cap[i] = 0;
0257 inheritable.cap[i] = 0;
0258 i++;
0259 }
0260
0261 effective.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0262 permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0263 inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
0264
0265 new = prepare_creds();
0266 if (!new)
0267 return -ENOMEM;
0268
0269 ret = security_capset(new, current_cred(),
0270 &effective, &inheritable, &permitted);
0271 if (ret < 0)
0272 goto error;
0273
0274 audit_log_capset(new, current_cred());
0275
0276 return commit_creds(new);
0277
0278 error:
0279 abort_creds(new);
0280 return ret;
0281 }
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 bool has_ns_capability(struct task_struct *t,
0295 struct user_namespace *ns, int cap)
0296 {
0297 int ret;
0298
0299 rcu_read_lock();
0300 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NONE);
0301 rcu_read_unlock();
0302
0303 return (ret == 0);
0304 }
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315
0316 bool has_capability(struct task_struct *t, int cap)
0317 {
0318 return has_ns_capability(t, &init_user_ns, cap);
0319 }
0320 EXPORT_SYMBOL(has_capability);
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335 bool has_ns_capability_noaudit(struct task_struct *t,
0336 struct user_namespace *ns, int cap)
0337 {
0338 int ret;
0339
0340 rcu_read_lock();
0341 ret = security_capable(__task_cred(t), ns, cap, CAP_OPT_NOAUDIT);
0342 rcu_read_unlock();
0343
0344 return (ret == 0);
0345 }
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359 bool has_capability_noaudit(struct task_struct *t, int cap)
0360 {
0361 return has_ns_capability_noaudit(t, &init_user_ns, cap);
0362 }
0363 EXPORT_SYMBOL(has_capability_noaudit);
0364
0365 static bool ns_capable_common(struct user_namespace *ns,
0366 int cap,
0367 unsigned int opts)
0368 {
0369 int capable;
0370
0371 if (unlikely(!cap_valid(cap))) {
0372 pr_crit("capable() called with invalid cap=%u\n", cap);
0373 BUG();
0374 }
0375
0376 capable = security_capable(current_cred(), ns, cap, opts);
0377 if (capable == 0) {
0378 current->flags |= PF_SUPERPRIV;
0379 return true;
0380 }
0381 return false;
0382 }
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 bool ns_capable(struct user_namespace *ns, int cap)
0396 {
0397 return ns_capable_common(ns, cap, CAP_OPT_NONE);
0398 }
0399 EXPORT_SYMBOL(ns_capable);
0400
0401
0402
0403
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413 bool ns_capable_noaudit(struct user_namespace *ns, int cap)
0414 {
0415 return ns_capable_common(ns, cap, CAP_OPT_NOAUDIT);
0416 }
0417 EXPORT_SYMBOL(ns_capable_noaudit);
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 bool ns_capable_setid(struct user_namespace *ns, int cap)
0433 {
0434 return ns_capable_common(ns, cap, CAP_OPT_INSETID);
0435 }
0436 EXPORT_SYMBOL(ns_capable_setid);
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448 bool capable(int cap)
0449 {
0450 return ns_capable(&init_user_ns, cap);
0451 }
0452 EXPORT_SYMBOL(capable);
0453 #endif
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464
0465
0466
0467 bool file_ns_capable(const struct file *file, struct user_namespace *ns,
0468 int cap)
0469 {
0470
0471 if (WARN_ON_ONCE(!cap_valid(cap)))
0472 return false;
0473
0474 if (security_capable(file->f_cred, ns, cap, CAP_OPT_NONE) == 0)
0475 return true;
0476
0477 return false;
0478 }
0479 EXPORT_SYMBOL(file_ns_capable);
0480
0481
0482
0483
0484
0485
0486
0487
0488 bool privileged_wrt_inode_uidgid(struct user_namespace *ns,
0489 struct user_namespace *mnt_userns,
0490 const struct inode *inode)
0491 {
0492 return kuid_has_mapping(ns, i_uid_into_mnt(mnt_userns, inode)) &&
0493 kgid_has_mapping(ns, i_gid_into_mnt(mnt_userns, inode));
0494 }
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505 bool capable_wrt_inode_uidgid(struct user_namespace *mnt_userns,
0506 const struct inode *inode, int cap)
0507 {
0508 struct user_namespace *ns = current_user_ns();
0509
0510 return ns_capable(ns, cap) &&
0511 privileged_wrt_inode_uidgid(ns, mnt_userns, inode);
0512 }
0513 EXPORT_SYMBOL(capable_wrt_inode_uidgid);
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523 bool ptracer_capable(struct task_struct *tsk, struct user_namespace *ns)
0524 {
0525 int ret = 0;
0526 const struct cred *cred;
0527
0528 rcu_read_lock();
0529 cred = rcu_dereference(tsk->ptracer_cred);
0530 if (cred)
0531 ret = security_capable(cred, ns, CAP_SYS_PTRACE,
0532 CAP_OPT_NOAUDIT);
0533 rcu_read_unlock();
0534 return (ret == 0);
0535 }