commoncap.c 28 KB

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