commoncap.c 27 KB

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