commoncap.c 28 KB

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