commoncap.c 31 KB

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