commoncap.c 26 KB

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