commoncap.c 32 KB

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