commoncap.c 28 KB

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