commoncap.c 32 KB

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