commoncap.c 27 KB

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