commoncap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /* Common capabilities, needed by capability.o.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. */
  9. #include <linux/capability.h>
  10. #include <linux/audit.h>
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/lsm_hooks.h>
  15. #include <linux/file.h>
  16. #include <linux/mm.h>
  17. #include <linux/mman.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/swap.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/netlink.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/xattr.h>
  24. #include <linux/hugetlb.h>
  25. #include <linux/mount.h>
  26. #include <linux/sched.h>
  27. #include <linux/prctl.h>
  28. #include <linux/securebits.h>
  29. #include <linux/user_namespace.h>
  30. #include <linux/binfmts.h>
  31. #include <linux/personality.h>
  32. /*
  33. * If a non-root user executes a setuid-root binary in
  34. * !secure(SECURE_NOROOT) mode, then we raise capabilities.
  35. * However if fE is also set, then the intent is for only
  36. * the file capabilities to be applied, and the setuid-root
  37. * bit is left on either to change the uid (plausible) or
  38. * to get full privilege on a kernel without file capabilities
  39. * support. So in that case we do not raise capabilities.
  40. *
  41. * Warn if that happens, once per boot.
  42. */
  43. static void warn_setuid_and_fcaps_mixed(const char *fname)
  44. {
  45. static int warned;
  46. if (!warned) {
  47. printk(KERN_INFO "warning: `%s' has both setuid-root and"
  48. " effective capabilities. Therefore not raising all"
  49. " capabilities.\n", fname);
  50. warned = 1;
  51. }
  52. }
  53. /**
  54. * cap_capable - Determine whether a task has a particular effective capability
  55. * @cred: The credentials to use
  56. * @ns: The user namespace in which we need the capability
  57. * @cap: The capability to check for
  58. * @audit: Whether to write an audit message or not
  59. *
  60. * Determine whether the nominated task has the specified capability amongst
  61. * its effective set, returning 0 if it does, -ve if it does not.
  62. *
  63. * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
  64. * and has_capability() functions. That is, it has the reverse semantics:
  65. * cap_has_capability() returns 0 when a task has a capability, but the
  66. * kernel's capable() and has_capability() returns 1 for this case.
  67. */
  68. int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
  69. int cap, int audit)
  70. {
  71. struct user_namespace *ns = targ_ns;
  72. /* See if cred has the capability in the target user namespace
  73. * by examining the target user namespace and all of the target
  74. * user namespace's parents.
  75. */
  76. for (;;) {
  77. /* Do we have the necessary capabilities? */
  78. if (ns == cred->user_ns)
  79. return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
  80. /* Have we tried all of the parent namespaces? */
  81. if (ns == &init_user_ns)
  82. return -EPERM;
  83. /*
  84. * The owner of the user namespace in the parent of the
  85. * user namespace has all caps.
  86. */
  87. if ((ns->parent == cred->user_ns) && uid_eq(ns->owner, cred->euid))
  88. return 0;
  89. /*
  90. * If you have a capability in a parent user ns, then you have
  91. * it over all children user namespaces as well.
  92. */
  93. ns = ns->parent;
  94. }
  95. /* We never get here */
  96. }
  97. /**
  98. * cap_settime - Determine whether the current process may set the system clock
  99. * @ts: The time to set
  100. * @tz: The timezone to set
  101. *
  102. * Determine whether the current process may set the system clock and timezone
  103. * information, returning 0 if permission granted, -ve if denied.
  104. */
  105. int cap_settime(const struct timespec *ts, const struct timezone *tz)
  106. {
  107. if (!capable(CAP_SYS_TIME))
  108. return -EPERM;
  109. return 0;
  110. }
  111. /**
  112. * cap_ptrace_access_check - Determine whether the current process may access
  113. * another
  114. * @child: The process to be accessed
  115. * @mode: The mode of attachment.
  116. *
  117. * If we are in the same or an ancestor user_ns and have all the target
  118. * task's capabilities, then ptrace access is allowed.
  119. * If we have the ptrace capability to the target user_ns, then ptrace
  120. * access is allowed.
  121. * Else denied.
  122. *
  123. * Determine whether a process may access another, returning 0 if permission
  124. * granted, -ve if denied.
  125. */
  126. int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
  127. {
  128. int ret = 0;
  129. const struct cred *cred, *child_cred;
  130. rcu_read_lock();
  131. cred = current_cred();
  132. child_cred = __task_cred(child);
  133. if (cred->user_ns == child_cred->user_ns &&
  134. cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
  135. goto out;
  136. if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
  137. goto out;
  138. ret = -EPERM;
  139. out:
  140. rcu_read_unlock();
  141. return ret;
  142. }
  143. /**
  144. * cap_ptrace_traceme - Determine whether another process may trace the current
  145. * @parent: The task proposed to be the tracer
  146. *
  147. * If parent is in the same or an ancestor user_ns and has all current's
  148. * capabilities, then ptrace access is allowed.
  149. * If parent has the ptrace capability to current's user_ns, then ptrace
  150. * access is allowed.
  151. * Else denied.
  152. *
  153. * Determine whether the nominated task is permitted to trace the current
  154. * process, returning 0 if permission is granted, -ve if denied.
  155. */
  156. int cap_ptrace_traceme(struct task_struct *parent)
  157. {
  158. int ret = 0;
  159. const struct cred *cred, *child_cred;
  160. rcu_read_lock();
  161. cred = __task_cred(parent);
  162. child_cred = current_cred();
  163. if (cred->user_ns == child_cred->user_ns &&
  164. cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
  165. goto out;
  166. if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
  167. goto out;
  168. ret = -EPERM;
  169. out:
  170. rcu_read_unlock();
  171. return ret;
  172. }
  173. /**
  174. * cap_capget - Retrieve a task's capability sets
  175. * @target: The task from which to retrieve the capability sets
  176. * @effective: The place to record the effective set
  177. * @inheritable: The place to record the inheritable set
  178. * @permitted: The place to record the permitted set
  179. *
  180. * This function retrieves the capabilities of the nominated task and returns
  181. * them to the caller.
  182. */
  183. int cap_capget(struct task_struct *target, kernel_cap_t *effective,
  184. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  185. {
  186. const struct cred *cred;
  187. /* Derived from kernel/capability.c:sys_capget. */
  188. rcu_read_lock();
  189. cred = __task_cred(target);
  190. *effective = cred->cap_effective;
  191. *inheritable = cred->cap_inheritable;
  192. *permitted = cred->cap_permitted;
  193. rcu_read_unlock();
  194. return 0;
  195. }
  196. /*
  197. * Determine whether the inheritable capabilities are limited to the old
  198. * permitted set. Returns 1 if they are limited, 0 if they are not.
  199. */
  200. static inline int cap_inh_is_capped(void)
  201. {
  202. /* they are so limited unless the current task has the CAP_SETPCAP
  203. * capability
  204. */
  205. if (cap_capable(current_cred(), current_cred()->user_ns,
  206. CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
  207. return 0;
  208. return 1;
  209. }
  210. /**
  211. * cap_capset - Validate and apply proposed changes to current's capabilities
  212. * @new: The proposed new credentials; alterations should be made here
  213. * @old: The current task's current credentials
  214. * @effective: A pointer to the proposed new effective capabilities set
  215. * @inheritable: A pointer to the proposed new inheritable capabilities set
  216. * @permitted: A pointer to the proposed new permitted capabilities set
  217. *
  218. * This function validates and applies a proposed mass change to the current
  219. * process's capability sets. The changes are made to the proposed new
  220. * credentials, and assuming no error, will be committed by the caller of LSM.
  221. */
  222. int cap_capset(struct cred *new,
  223. const struct cred *old,
  224. const kernel_cap_t *effective,
  225. const kernel_cap_t *inheritable,
  226. const kernel_cap_t *permitted)
  227. {
  228. if (cap_inh_is_capped() &&
  229. !cap_issubset(*inheritable,
  230. cap_combine(old->cap_inheritable,
  231. old->cap_permitted)))
  232. /* incapable of using this inheritable set */
  233. return -EPERM;
  234. if (!cap_issubset(*inheritable,
  235. cap_combine(old->cap_inheritable,
  236. old->cap_bset)))
  237. /* no new pI capabilities outside bounding set */
  238. return -EPERM;
  239. /* verify restrictions on target's new Permitted set */
  240. if (!cap_issubset(*permitted, old->cap_permitted))
  241. return -EPERM;
  242. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  243. if (!cap_issubset(*effective, *permitted))
  244. return -EPERM;
  245. new->cap_effective = *effective;
  246. new->cap_inheritable = *inheritable;
  247. new->cap_permitted = *permitted;
  248. return 0;
  249. }
  250. /*
  251. * Clear proposed capability sets for execve().
  252. */
  253. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  254. {
  255. cap_clear(bprm->cred->cap_permitted);
  256. bprm->cap_effective = false;
  257. }
  258. /**
  259. * cap_inode_need_killpriv - Determine if inode change affects privileges
  260. * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
  261. *
  262. * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
  263. * affects the security markings on that inode, and if it is, should
  264. * inode_killpriv() be invoked or the change rejected?
  265. *
  266. * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
  267. * -ve to deny the change.
  268. */
  269. int cap_inode_need_killpriv(struct dentry *dentry)
  270. {
  271. struct inode *inode = d_backing_inode(dentry);
  272. int error;
  273. if (!inode->i_op->getxattr)
  274. return 0;
  275. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  276. if (error <= 0)
  277. return 0;
  278. return 1;
  279. }
  280. /**
  281. * cap_inode_killpriv - Erase the security markings on an inode
  282. * @dentry: The inode/dentry to alter
  283. *
  284. * Erase the privilege-enhancing security markings on an inode.
  285. *
  286. * Returns 0 if successful, -ve on error.
  287. */
  288. int cap_inode_killpriv(struct dentry *dentry)
  289. {
  290. struct inode *inode = d_backing_inode(dentry);
  291. if (!inode->i_op->removexattr)
  292. return 0;
  293. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  294. }
  295. /*
  296. * Calculate the new process capability sets from the capability sets attached
  297. * to a file.
  298. */
  299. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  300. struct linux_binprm *bprm,
  301. bool *effective,
  302. bool *has_cap)
  303. {
  304. struct cred *new = bprm->cred;
  305. unsigned i;
  306. int ret = 0;
  307. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  308. *effective = true;
  309. if (caps->magic_etc & VFS_CAP_REVISION_MASK)
  310. *has_cap = true;
  311. CAP_FOR_EACH_U32(i) {
  312. __u32 permitted = caps->permitted.cap[i];
  313. __u32 inheritable = caps->inheritable.cap[i];
  314. /*
  315. * pP' = (X & fP) | (pI & fI)
  316. */
  317. new->cap_permitted.cap[i] =
  318. (new->cap_bset.cap[i] & permitted) |
  319. (new->cap_inheritable.cap[i] & inheritable);
  320. if (permitted & ~new->cap_permitted.cap[i])
  321. /* insufficient to execute correctly */
  322. ret = -EPERM;
  323. }
  324. /*
  325. * For legacy apps, with no internal support for recognizing they
  326. * do not have enough capabilities, we return an error if they are
  327. * missing some "forced" (aka file-permitted) capabilities.
  328. */
  329. return *effective ? ret : 0;
  330. }
  331. /*
  332. * Extract the on-exec-apply capability sets for an executable file.
  333. */
  334. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  335. {
  336. struct inode *inode = d_backing_inode(dentry);
  337. __u32 magic_etc;
  338. unsigned tocopy, i;
  339. int size;
  340. struct vfs_cap_data caps;
  341. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  342. if (!inode || !inode->i_op->getxattr)
  343. return -ENODATA;
  344. size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
  345. XATTR_CAPS_SZ);
  346. if (size == -ENODATA || size == -EOPNOTSUPP)
  347. /* no data, that's ok */
  348. return -ENODATA;
  349. if (size < 0)
  350. return size;
  351. if (size < sizeof(magic_etc))
  352. return -EINVAL;
  353. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
  354. switch (magic_etc & VFS_CAP_REVISION_MASK) {
  355. case VFS_CAP_REVISION_1:
  356. if (size != XATTR_CAPS_SZ_1)
  357. return -EINVAL;
  358. tocopy = VFS_CAP_U32_1;
  359. break;
  360. case VFS_CAP_REVISION_2:
  361. if (size != XATTR_CAPS_SZ_2)
  362. return -EINVAL;
  363. tocopy = VFS_CAP_U32_2;
  364. break;
  365. default:
  366. return -EINVAL;
  367. }
  368. CAP_FOR_EACH_U32(i) {
  369. if (i >= tocopy)
  370. break;
  371. cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
  372. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
  373. }
  374. cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  375. cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  376. return 0;
  377. }
  378. /*
  379. * Attempt to get the on-exec apply capability sets for an executable file from
  380. * its xattrs and, if present, apply them to the proposed credentials being
  381. * constructed by execve().
  382. */
  383. static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_cap)
  384. {
  385. int rc = 0;
  386. struct cpu_vfs_cap_data vcaps;
  387. bprm_clear_caps(bprm);
  388. if (!file_caps_enabled)
  389. return 0;
  390. if (bprm->file->f_path.mnt->mnt_flags & MNT_NOSUID)
  391. return 0;
  392. rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
  393. if (rc < 0) {
  394. if (rc == -EINVAL)
  395. printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
  396. __func__, rc, bprm->filename);
  397. else if (rc == -ENODATA)
  398. rc = 0;
  399. goto out;
  400. }
  401. rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_cap);
  402. if (rc == -EINVAL)
  403. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  404. __func__, rc, bprm->filename);
  405. out:
  406. if (rc)
  407. bprm_clear_caps(bprm);
  408. return rc;
  409. }
  410. /**
  411. * cap_bprm_set_creds - Set up the proposed credentials for execve().
  412. * @bprm: The execution parameters, including the proposed creds
  413. *
  414. * Set up the proposed credentials for a new execution context being
  415. * constructed by execve(). The proposed creds in @bprm->cred is altered,
  416. * which won't take effect immediately. Returns 0 if successful, -ve on error.
  417. */
  418. int cap_bprm_set_creds(struct linux_binprm *bprm)
  419. {
  420. const struct cred *old = current_cred();
  421. struct cred *new = bprm->cred;
  422. bool effective, has_cap = false;
  423. int ret;
  424. kuid_t root_uid;
  425. effective = false;
  426. ret = get_file_caps(bprm, &effective, &has_cap);
  427. if (ret < 0)
  428. return ret;
  429. root_uid = make_kuid(new->user_ns, 0);
  430. if (!issecure(SECURE_NOROOT)) {
  431. /*
  432. * If the legacy file capability is set, then don't set privs
  433. * for a setuid root binary run by a non-root user. Do set it
  434. * for a root user just to cause least surprise to an admin.
  435. */
  436. if (has_cap && !uid_eq(new->uid, root_uid) && uid_eq(new->euid, root_uid)) {
  437. warn_setuid_and_fcaps_mixed(bprm->filename);
  438. goto skip;
  439. }
  440. /*
  441. * To support inheritance of root-permissions and suid-root
  442. * executables under compatibility mode, we override the
  443. * capability sets for the file.
  444. *
  445. * If only the real uid is 0, we do not set the effective bit.
  446. */
  447. if (uid_eq(new->euid, root_uid) || uid_eq(new->uid, root_uid)) {
  448. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  449. new->cap_permitted = cap_combine(old->cap_bset,
  450. old->cap_inheritable);
  451. }
  452. if (uid_eq(new->euid, root_uid))
  453. effective = true;
  454. }
  455. skip:
  456. /* if we have fs caps, clear dangerous personality flags */
  457. if (!cap_issubset(new->cap_permitted, old->cap_permitted))
  458. bprm->per_clear |= PER_CLEAR_ON_SETID;
  459. /* Don't let someone trace a set[ug]id/setpcap binary with the revised
  460. * credentials unless they have the appropriate permit.
  461. *
  462. * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
  463. */
  464. if ((!uid_eq(new->euid, old->uid) ||
  465. !gid_eq(new->egid, old->gid) ||
  466. !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
  467. bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  468. /* downgrade; they get no more than they had, and maybe less */
  469. if (!capable(CAP_SETUID) ||
  470. (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
  471. new->euid = new->uid;
  472. new->egid = new->gid;
  473. }
  474. new->cap_permitted = cap_intersect(new->cap_permitted,
  475. old->cap_permitted);
  476. }
  477. new->suid = new->fsuid = new->euid;
  478. new->sgid = new->fsgid = new->egid;
  479. if (effective)
  480. new->cap_effective = new->cap_permitted;
  481. else
  482. cap_clear(new->cap_effective);
  483. bprm->cap_effective = effective;
  484. /*
  485. * Audit candidate if current->cap_effective is set
  486. *
  487. * We do not bother to audit if 3 things are true:
  488. * 1) cap_effective has all caps
  489. * 2) we are root
  490. * 3) root is supposed to have all caps (SECURE_NOROOT)
  491. * Since this is just a normal root execing a process.
  492. *
  493. * Number 1 above might fail if you don't have a full bset, but I think
  494. * that is interesting information to audit.
  495. */
  496. if (!cap_isclear(new->cap_effective)) {
  497. if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
  498. !uid_eq(new->euid, root_uid) || !uid_eq(new->uid, root_uid) ||
  499. issecure(SECURE_NOROOT)) {
  500. ret = audit_log_bprm_fcaps(bprm, new, old);
  501. if (ret < 0)
  502. return ret;
  503. }
  504. }
  505. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  506. return 0;
  507. }
  508. /**
  509. * cap_bprm_secureexec - Determine whether a secure execution is required
  510. * @bprm: The execution parameters
  511. *
  512. * Determine whether a secure execution is required, return 1 if it is, and 0
  513. * if it is not.
  514. *
  515. * The credentials have been committed by this point, and so are no longer
  516. * available through @bprm->cred.
  517. */
  518. int cap_bprm_secureexec(struct linux_binprm *bprm)
  519. {
  520. const struct cred *cred = current_cred();
  521. kuid_t root_uid = make_kuid(cred->user_ns, 0);
  522. if (!uid_eq(cred->uid, root_uid)) {
  523. if (bprm->cap_effective)
  524. return 1;
  525. if (!cap_isclear(cred->cap_permitted))
  526. return 1;
  527. }
  528. return (!uid_eq(cred->euid, cred->uid) ||
  529. !gid_eq(cred->egid, cred->gid));
  530. }
  531. /**
  532. * cap_inode_setxattr - Determine whether an xattr may be altered
  533. * @dentry: The inode/dentry being altered
  534. * @name: The name of the xattr to be changed
  535. * @value: The value that the xattr will be changed to
  536. * @size: The size of value
  537. * @flags: The replacement flag
  538. *
  539. * Determine whether an xattr may be altered or set on an inode, returning 0 if
  540. * permission is granted, -ve if denied.
  541. *
  542. * This is used to make sure security xattrs don't get updated or set by those
  543. * who aren't privileged to do so.
  544. */
  545. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  546. const void *value, size_t size, int flags)
  547. {
  548. if (!strcmp(name, XATTR_NAME_CAPS)) {
  549. if (!capable(CAP_SETFCAP))
  550. return -EPERM;
  551. return 0;
  552. }
  553. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  554. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  555. !capable(CAP_SYS_ADMIN))
  556. return -EPERM;
  557. return 0;
  558. }
  559. /**
  560. * cap_inode_removexattr - Determine whether an xattr may be removed
  561. * @dentry: The inode/dentry being altered
  562. * @name: The name of the xattr to be changed
  563. *
  564. * Determine whether an xattr may be removed from an inode, returning 0 if
  565. * permission is granted, -ve if denied.
  566. *
  567. * This is used to make sure security xattrs don't get removed by those who
  568. * aren't privileged to remove them.
  569. */
  570. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  571. {
  572. if (!strcmp(name, XATTR_NAME_CAPS)) {
  573. if (!capable(CAP_SETFCAP))
  574. return -EPERM;
  575. return 0;
  576. }
  577. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  578. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  579. !capable(CAP_SYS_ADMIN))
  580. return -EPERM;
  581. return 0;
  582. }
  583. /*
  584. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  585. * a process after a call to setuid, setreuid, or setresuid.
  586. *
  587. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  588. * {r,e,s}uid != 0, the permitted and effective capabilities are
  589. * cleared.
  590. *
  591. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  592. * capabilities of the process are cleared.
  593. *
  594. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  595. * capabilities are set to the permitted capabilities.
  596. *
  597. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  598. * never happen.
  599. *
  600. * -astor
  601. *
  602. * cevans - New behaviour, Oct '99
  603. * A process may, via prctl(), elect to keep its capabilities when it
  604. * calls setuid() and switches away from uid==0. Both permitted and
  605. * effective sets will be retained.
  606. * Without this change, it was impossible for a daemon to drop only some
  607. * of its privilege. The call to setuid(!=0) would drop all privileges!
  608. * Keeping uid 0 is not an option because uid 0 owns too many vital
  609. * files..
  610. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  611. */
  612. static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
  613. {
  614. kuid_t root_uid = make_kuid(old->user_ns, 0);
  615. if ((uid_eq(old->uid, root_uid) ||
  616. uid_eq(old->euid, root_uid) ||
  617. uid_eq(old->suid, root_uid)) &&
  618. (!uid_eq(new->uid, root_uid) &&
  619. !uid_eq(new->euid, root_uid) &&
  620. !uid_eq(new->suid, root_uid)) &&
  621. !issecure(SECURE_KEEP_CAPS)) {
  622. cap_clear(new->cap_permitted);
  623. cap_clear(new->cap_effective);
  624. }
  625. if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
  626. cap_clear(new->cap_effective);
  627. if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
  628. new->cap_effective = new->cap_permitted;
  629. }
  630. /**
  631. * cap_task_fix_setuid - Fix up the results of setuid() call
  632. * @new: The proposed credentials
  633. * @old: The current task's current credentials
  634. * @flags: Indications of what has changed
  635. *
  636. * Fix up the results of setuid() call before the credential changes are
  637. * actually applied, returning 0 to grant the changes, -ve to deny them.
  638. */
  639. int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
  640. {
  641. switch (flags) {
  642. case LSM_SETID_RE:
  643. case LSM_SETID_ID:
  644. case LSM_SETID_RES:
  645. /* juggle the capabilities to follow [RES]UID changes unless
  646. * otherwise suppressed */
  647. if (!issecure(SECURE_NO_SETUID_FIXUP))
  648. cap_emulate_setxuid(new, old);
  649. break;
  650. case LSM_SETID_FS:
  651. /* juggle the capabilties to follow FSUID changes, unless
  652. * otherwise suppressed
  653. *
  654. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  655. * if not, we might be a bit too harsh here.
  656. */
  657. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  658. kuid_t root_uid = make_kuid(old->user_ns, 0);
  659. if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
  660. new->cap_effective =
  661. cap_drop_fs_set(new->cap_effective);
  662. if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
  663. new->cap_effective =
  664. cap_raise_fs_set(new->cap_effective,
  665. new->cap_permitted);
  666. }
  667. break;
  668. default:
  669. return -EINVAL;
  670. }
  671. return 0;
  672. }
  673. /*
  674. * Rationale: code calling task_setscheduler, task_setioprio, and
  675. * task_setnice, assumes that
  676. * . if capable(cap_sys_nice), then those actions should be allowed
  677. * . if not capable(cap_sys_nice), but acting on your own processes,
  678. * then those actions should be allowed
  679. * This is insufficient now since you can call code without suid, but
  680. * yet with increased caps.
  681. * So we check for increased caps on the target process.
  682. */
  683. static int cap_safe_nice(struct task_struct *p)
  684. {
  685. int is_subset, ret = 0;
  686. rcu_read_lock();
  687. is_subset = cap_issubset(__task_cred(p)->cap_permitted,
  688. current_cred()->cap_permitted);
  689. if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
  690. ret = -EPERM;
  691. rcu_read_unlock();
  692. return ret;
  693. }
  694. /**
  695. * cap_task_setscheduler - Detemine if scheduler policy change is permitted
  696. * @p: The task to affect
  697. *
  698. * Detemine if the requested scheduler policy change is permitted for the
  699. * specified task, returning 0 if permission is granted, -ve if denied.
  700. */
  701. int cap_task_setscheduler(struct task_struct *p)
  702. {
  703. return cap_safe_nice(p);
  704. }
  705. /**
  706. * cap_task_ioprio - Detemine if I/O priority change is permitted
  707. * @p: The task to affect
  708. * @ioprio: The I/O priority to set
  709. *
  710. * Detemine if the requested I/O priority change is permitted for the specified
  711. * task, returning 0 if permission is granted, -ve if denied.
  712. */
  713. int cap_task_setioprio(struct task_struct *p, int ioprio)
  714. {
  715. return cap_safe_nice(p);
  716. }
  717. /**
  718. * cap_task_ioprio - Detemine if task priority change is permitted
  719. * @p: The task to affect
  720. * @nice: The nice value to set
  721. *
  722. * Detemine if the requested task priority change is permitted for the
  723. * specified task, returning 0 if permission is granted, -ve if denied.
  724. */
  725. int cap_task_setnice(struct task_struct *p, int nice)
  726. {
  727. return cap_safe_nice(p);
  728. }
  729. /*
  730. * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
  731. * the current task's bounding set. Returns 0 on success, -ve on error.
  732. */
  733. static int cap_prctl_drop(unsigned long cap)
  734. {
  735. struct cred *new;
  736. if (!ns_capable(current_user_ns(), CAP_SETPCAP))
  737. return -EPERM;
  738. if (!cap_valid(cap))
  739. return -EINVAL;
  740. new = prepare_creds();
  741. if (!new)
  742. return -ENOMEM;
  743. cap_lower(new->cap_bset, cap);
  744. return commit_creds(new);
  745. }
  746. /**
  747. * cap_task_prctl - Implement process control functions for this security module
  748. * @option: The process control function requested
  749. * @arg2, @arg3, @arg4, @arg5: The argument data for this function
  750. *
  751. * Allow process control functions (sys_prctl()) to alter capabilities; may
  752. * also deny access to other functions not otherwise implemented here.
  753. *
  754. * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
  755. * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
  756. * modules will consider performing the function.
  757. */
  758. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  759. unsigned long arg4, unsigned long arg5)
  760. {
  761. const struct cred *old = current_cred();
  762. struct cred *new;
  763. switch (option) {
  764. case PR_CAPBSET_READ:
  765. if (!cap_valid(arg2))
  766. return -EINVAL;
  767. return !!cap_raised(old->cap_bset, arg2);
  768. case PR_CAPBSET_DROP:
  769. return cap_prctl_drop(arg2);
  770. /*
  771. * The next four prctl's remain to assist with transitioning a
  772. * system from legacy UID=0 based privilege (when filesystem
  773. * capabilities are not in use) to a system using filesystem
  774. * capabilities only - as the POSIX.1e draft intended.
  775. *
  776. * Note:
  777. *
  778. * PR_SET_SECUREBITS =
  779. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  780. * | issecure_mask(SECURE_NOROOT)
  781. * | issecure_mask(SECURE_NOROOT_LOCKED)
  782. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  783. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  784. *
  785. * will ensure that the current process and all of its
  786. * children will be locked into a pure
  787. * capability-based-privilege environment.
  788. */
  789. case PR_SET_SECUREBITS:
  790. if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
  791. & (old->securebits ^ arg2)) /*[1]*/
  792. || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
  793. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  794. || (cap_capable(current_cred(),
  795. current_cred()->user_ns, CAP_SETPCAP,
  796. SECURITY_CAP_AUDIT) != 0) /*[4]*/
  797. /*
  798. * [1] no changing of bits that are locked
  799. * [2] no unlocking of locks
  800. * [3] no setting of unsupported bits
  801. * [4] doing anything requires privilege (go read about
  802. * the "sendmail capabilities bug")
  803. */
  804. )
  805. /* cannot change a locked bit */
  806. return -EPERM;
  807. new = prepare_creds();
  808. if (!new)
  809. return -ENOMEM;
  810. new->securebits = arg2;
  811. return commit_creds(new);
  812. case PR_GET_SECUREBITS:
  813. return old->securebits;
  814. case PR_GET_KEEPCAPS:
  815. return !!issecure(SECURE_KEEP_CAPS);
  816. case PR_SET_KEEPCAPS:
  817. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  818. return -EINVAL;
  819. if (issecure(SECURE_KEEP_CAPS_LOCKED))
  820. return -EPERM;
  821. new = prepare_creds();
  822. if (!new)
  823. return -ENOMEM;
  824. if (arg2)
  825. new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  826. else
  827. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  828. return commit_creds(new);
  829. default:
  830. /* No functionality available - continue with default */
  831. return -ENOSYS;
  832. }
  833. }
  834. /**
  835. * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
  836. * @mm: The VM space in which the new mapping is to be made
  837. * @pages: The size of the mapping
  838. *
  839. * Determine whether the allocation of a new virtual mapping by the current
  840. * task is permitted, returning 1 if permission is granted, 0 if not.
  841. */
  842. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  843. {
  844. int cap_sys_admin = 0;
  845. if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
  846. SECURITY_CAP_NOAUDIT) == 0)
  847. cap_sys_admin = 1;
  848. return cap_sys_admin;
  849. }
  850. /*
  851. * cap_mmap_addr - check if able to map given addr
  852. * @addr: address attempting to be mapped
  853. *
  854. * If the process is attempting to map memory below dac_mmap_min_addr they need
  855. * CAP_SYS_RAWIO. The other parameters to this function are unused by the
  856. * capability security module. Returns 0 if this mapping should be allowed
  857. * -EPERM if not.
  858. */
  859. int cap_mmap_addr(unsigned long addr)
  860. {
  861. int ret = 0;
  862. if (addr < dac_mmap_min_addr) {
  863. ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
  864. SECURITY_CAP_AUDIT);
  865. /* set PF_SUPERPRIV if it turns out we allow the low mmap */
  866. if (ret == 0)
  867. current->flags |= PF_SUPERPRIV;
  868. }
  869. return ret;
  870. }
  871. int cap_mmap_file(struct file *file, unsigned long reqprot,
  872. unsigned long prot, unsigned long flags)
  873. {
  874. return 0;
  875. }
  876. #ifdef CONFIG_SECURITY
  877. struct security_hook_list capability_hooks[] = {
  878. LSM_HOOK_INIT(capable, cap_capable),
  879. LSM_HOOK_INIT(settime, cap_settime),
  880. LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
  881. LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
  882. LSM_HOOK_INIT(capget, cap_capget),
  883. LSM_HOOK_INIT(capset, cap_capset),
  884. LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
  885. LSM_HOOK_INIT(bprm_secureexec, cap_bprm_secureexec),
  886. LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
  887. LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
  888. LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
  889. LSM_HOOK_INIT(mmap_file, cap_mmap_file),
  890. LSM_HOOK_INIT(task_fix_setuid, cap_task_fix_setuid),
  891. LSM_HOOK_INIT(task_prctl, cap_task_prctl),
  892. LSM_HOOK_INIT(task_setscheduler, cap_task_setscheduler),
  893. LSM_HOOK_INIT(task_setioprio, cap_task_setioprio),
  894. LSM_HOOK_INIT(task_setnice, cap_task_setnice),
  895. LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
  896. };
  897. void __init capability_add_hooks(void)
  898. {
  899. security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks));
  900. }
  901. #endif /* CONFIG_SECURITY */