commoncap.c 27 KB

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