commoncap.c 31 KB

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