commoncap.c 27 KB

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