commoncap.c 27 KB

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