commoncap.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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. /*
  81. * If we're already at a lower level than we're looking for,
  82. * we're done searching.
  83. */
  84. if (ns->level <= cred->user_ns->level)
  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. /**
  101. * cap_settime - Determine whether the current process may set the system clock
  102. * @ts: The time to set
  103. * @tz: The timezone to set
  104. *
  105. * Determine whether the current process may set the system clock and timezone
  106. * information, returning 0 if permission granted, -ve if denied.
  107. */
  108. int cap_settime(const struct timespec64 *ts, const struct timezone *tz)
  109. {
  110. if (!capable(CAP_SYS_TIME))
  111. return -EPERM;
  112. return 0;
  113. }
  114. /**
  115. * cap_ptrace_access_check - Determine whether the current process may access
  116. * another
  117. * @child: The process to be accessed
  118. * @mode: The mode of attachment.
  119. *
  120. * If we are in the same or an ancestor user_ns and have all the target
  121. * task's capabilities, then ptrace access is allowed.
  122. * If we have the ptrace capability to the target user_ns, then ptrace
  123. * access is allowed.
  124. * Else denied.
  125. *
  126. * Determine whether a process may access another, returning 0 if permission
  127. * granted, -ve if denied.
  128. */
  129. int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
  130. {
  131. int ret = 0;
  132. const struct cred *cred, *child_cred;
  133. const kernel_cap_t *caller_caps;
  134. rcu_read_lock();
  135. cred = current_cred();
  136. child_cred = __task_cred(child);
  137. if (mode & PTRACE_MODE_FSCREDS)
  138. caller_caps = &cred->cap_effective;
  139. else
  140. caller_caps = &cred->cap_permitted;
  141. if (cred->user_ns == child_cred->user_ns &&
  142. cap_issubset(child_cred->cap_permitted, *caller_caps))
  143. goto out;
  144. if (ns_capable(child_cred->user_ns, CAP_SYS_PTRACE))
  145. goto out;
  146. ret = -EPERM;
  147. out:
  148. rcu_read_unlock();
  149. return ret;
  150. }
  151. /**
  152. * cap_ptrace_traceme - Determine whether another process may trace the current
  153. * @parent: The task proposed to be the tracer
  154. *
  155. * If parent is in the same or an ancestor user_ns and has all current's
  156. * capabilities, then ptrace access is allowed.
  157. * If parent has the ptrace capability to current's user_ns, then ptrace
  158. * access is allowed.
  159. * Else denied.
  160. *
  161. * Determine whether the nominated task is permitted to trace the current
  162. * process, returning 0 if permission is granted, -ve if denied.
  163. */
  164. int cap_ptrace_traceme(struct task_struct *parent)
  165. {
  166. int ret = 0;
  167. const struct cred *cred, *child_cred;
  168. rcu_read_lock();
  169. cred = __task_cred(parent);
  170. child_cred = current_cred();
  171. if (cred->user_ns == child_cred->user_ns &&
  172. cap_issubset(child_cred->cap_permitted, cred->cap_permitted))
  173. goto out;
  174. if (has_ns_capability(parent, child_cred->user_ns, CAP_SYS_PTRACE))
  175. goto out;
  176. ret = -EPERM;
  177. out:
  178. rcu_read_unlock();
  179. return ret;
  180. }
  181. /**
  182. * cap_capget - Retrieve a task's capability sets
  183. * @target: The task from which to retrieve the capability sets
  184. * @effective: The place to record the effective set
  185. * @inheritable: The place to record the inheritable set
  186. * @permitted: The place to record the permitted set
  187. *
  188. * This function retrieves the capabilities of the nominated task and returns
  189. * them to the caller.
  190. */
  191. int cap_capget(struct task_struct *target, kernel_cap_t *effective,
  192. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  193. {
  194. const struct cred *cred;
  195. /* Derived from kernel/capability.c:sys_capget. */
  196. rcu_read_lock();
  197. cred = __task_cred(target);
  198. *effective = cred->cap_effective;
  199. *inheritable = cred->cap_inheritable;
  200. *permitted = cred->cap_permitted;
  201. rcu_read_unlock();
  202. return 0;
  203. }
  204. /*
  205. * Determine whether the inheritable capabilities are limited to the old
  206. * permitted set. Returns 1 if they are limited, 0 if they are not.
  207. */
  208. static inline int cap_inh_is_capped(void)
  209. {
  210. /* they are so limited unless the current task has the CAP_SETPCAP
  211. * capability
  212. */
  213. if (cap_capable(current_cred(), current_cred()->user_ns,
  214. CAP_SETPCAP, SECURITY_CAP_AUDIT) == 0)
  215. return 0;
  216. return 1;
  217. }
  218. /**
  219. * cap_capset - Validate and apply proposed changes to current's capabilities
  220. * @new: The proposed new credentials; alterations should be made here
  221. * @old: The current task's current credentials
  222. * @effective: A pointer to the proposed new effective capabilities set
  223. * @inheritable: A pointer to the proposed new inheritable capabilities set
  224. * @permitted: A pointer to the proposed new permitted capabilities set
  225. *
  226. * This function validates and applies a proposed mass change to the current
  227. * process's capability sets. The changes are made to the proposed new
  228. * credentials, and assuming no error, will be committed by the caller of LSM.
  229. */
  230. int cap_capset(struct cred *new,
  231. const struct cred *old,
  232. const kernel_cap_t *effective,
  233. const kernel_cap_t *inheritable,
  234. const kernel_cap_t *permitted)
  235. {
  236. if (cap_inh_is_capped() &&
  237. !cap_issubset(*inheritable,
  238. cap_combine(old->cap_inheritable,
  239. old->cap_permitted)))
  240. /* incapable of using this inheritable set */
  241. return -EPERM;
  242. if (!cap_issubset(*inheritable,
  243. cap_combine(old->cap_inheritable,
  244. old->cap_bset)))
  245. /* no new pI capabilities outside bounding set */
  246. return -EPERM;
  247. /* verify restrictions on target's new Permitted set */
  248. if (!cap_issubset(*permitted, old->cap_permitted))
  249. return -EPERM;
  250. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  251. if (!cap_issubset(*effective, *permitted))
  252. return -EPERM;
  253. new->cap_effective = *effective;
  254. new->cap_inheritable = *inheritable;
  255. new->cap_permitted = *permitted;
  256. /*
  257. * Mask off ambient bits that are no longer both permitted and
  258. * inheritable.
  259. */
  260. new->cap_ambient = cap_intersect(new->cap_ambient,
  261. cap_intersect(*permitted,
  262. *inheritable));
  263. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  264. return -EINVAL;
  265. return 0;
  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 1 if security.capability has a value, meaning inode_killpriv()
  276. * is required, 0 otherwise, meaning inode_killpriv() is not required.
  277. */
  278. int cap_inode_need_killpriv(struct dentry *dentry)
  279. {
  280. struct inode *inode = d_backing_inode(dentry);
  281. int error;
  282. error = __vfs_getxattr(dentry, inode, XATTR_NAME_CAPS, NULL, 0);
  283. return error > 0;
  284. }
  285. /**
  286. * cap_inode_killpriv - Erase the security markings on an inode
  287. * @dentry: The inode/dentry to alter
  288. *
  289. * Erase the privilege-enhancing security markings on an inode.
  290. *
  291. * Returns 0 if successful, -ve on error.
  292. */
  293. int cap_inode_killpriv(struct dentry *dentry)
  294. {
  295. int error;
  296. error = __vfs_removexattr(dentry, XATTR_NAME_CAPS);
  297. if (error == -EOPNOTSUPP)
  298. error = 0;
  299. return error;
  300. }
  301. static bool rootid_owns_currentns(kuid_t kroot)
  302. {
  303. struct user_namespace *ns;
  304. if (!uid_valid(kroot))
  305. return false;
  306. for (ns = current_user_ns(); ; ns = ns->parent) {
  307. if (from_kuid(ns, kroot) == 0)
  308. return true;
  309. if (ns == &init_user_ns)
  310. break;
  311. }
  312. return false;
  313. }
  314. static __u32 sansflags(__u32 m)
  315. {
  316. return m & ~VFS_CAP_FLAGS_EFFECTIVE;
  317. }
  318. static bool is_v2header(size_t size, const struct vfs_cap_data *cap)
  319. {
  320. if (size != XATTR_CAPS_SZ_2)
  321. return false;
  322. return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_2;
  323. }
  324. static bool is_v3header(size_t size, const struct vfs_cap_data *cap)
  325. {
  326. if (size != XATTR_CAPS_SZ_3)
  327. return false;
  328. return sansflags(le32_to_cpu(cap->magic_etc)) == VFS_CAP_REVISION_3;
  329. }
  330. /*
  331. * getsecurity: We are called for security.* before any attempt to read the
  332. * xattr from the inode itself.
  333. *
  334. * This gives us a chance to read the on-disk value and convert it. If we
  335. * return -EOPNOTSUPP, then vfs_getxattr() will call the i_op handler.
  336. *
  337. * Note we are not called by vfs_getxattr_alloc(), but that is only called
  338. * by the integrity subsystem, which really wants the unconverted values -
  339. * so that's good.
  340. */
  341. int cap_inode_getsecurity(struct inode *inode, const char *name, void **buffer,
  342. bool alloc)
  343. {
  344. int size, ret;
  345. kuid_t kroot;
  346. uid_t root, mappedroot;
  347. char *tmpbuf = NULL;
  348. struct vfs_cap_data *cap;
  349. struct vfs_ns_cap_data *nscap;
  350. struct dentry *dentry;
  351. struct user_namespace *fs_ns;
  352. if (strcmp(name, "capability") != 0)
  353. return -EOPNOTSUPP;
  354. dentry = d_find_any_alias(inode);
  355. if (!dentry)
  356. return -EINVAL;
  357. size = sizeof(struct vfs_ns_cap_data);
  358. ret = (int) vfs_getxattr_alloc(dentry, XATTR_NAME_CAPS,
  359. &tmpbuf, size, GFP_NOFS);
  360. dput(dentry);
  361. if (ret < 0)
  362. return ret;
  363. fs_ns = inode->i_sb->s_user_ns;
  364. cap = (struct vfs_cap_data *) tmpbuf;
  365. if (is_v2header((size_t) ret, cap)) {
  366. /* If this is sizeof(vfs_cap_data) then we're ok with the
  367. * on-disk value, so return that. */
  368. if (alloc)
  369. *buffer = tmpbuf;
  370. else
  371. kfree(tmpbuf);
  372. return ret;
  373. } else if (!is_v3header((size_t) ret, cap)) {
  374. kfree(tmpbuf);
  375. return -EINVAL;
  376. }
  377. nscap = (struct vfs_ns_cap_data *) tmpbuf;
  378. root = le32_to_cpu(nscap->rootid);
  379. kroot = make_kuid(fs_ns, root);
  380. /* If the root kuid maps to a valid uid in current ns, then return
  381. * this as a nscap. */
  382. mappedroot = from_kuid(current_user_ns(), kroot);
  383. if (mappedroot != (uid_t)-1 && mappedroot != (uid_t)0) {
  384. if (alloc) {
  385. *buffer = tmpbuf;
  386. nscap->rootid = cpu_to_le32(mappedroot);
  387. } else
  388. kfree(tmpbuf);
  389. return size;
  390. }
  391. if (!rootid_owns_currentns(kroot)) {
  392. kfree(tmpbuf);
  393. return -EOPNOTSUPP;
  394. }
  395. /* This comes from a parent namespace. Return as a v2 capability */
  396. size = sizeof(struct vfs_cap_data);
  397. if (alloc) {
  398. *buffer = kmalloc(size, GFP_ATOMIC);
  399. if (*buffer) {
  400. struct vfs_cap_data *cap = *buffer;
  401. __le32 nsmagic, magic;
  402. magic = VFS_CAP_REVISION_2;
  403. nsmagic = le32_to_cpu(nscap->magic_etc);
  404. if (nsmagic & VFS_CAP_FLAGS_EFFECTIVE)
  405. magic |= VFS_CAP_FLAGS_EFFECTIVE;
  406. memcpy(&cap->data, &nscap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
  407. cap->magic_etc = cpu_to_le32(magic);
  408. } else {
  409. size = -ENOMEM;
  410. }
  411. }
  412. kfree(tmpbuf);
  413. return size;
  414. }
  415. static kuid_t rootid_from_xattr(const void *value, size_t size,
  416. struct user_namespace *task_ns)
  417. {
  418. const struct vfs_ns_cap_data *nscap = value;
  419. uid_t rootid = 0;
  420. if (size == XATTR_CAPS_SZ_3)
  421. rootid = le32_to_cpu(nscap->rootid);
  422. return make_kuid(task_ns, rootid);
  423. }
  424. static bool validheader(size_t size, const struct vfs_cap_data *cap)
  425. {
  426. return is_v2header(size, cap) || is_v3header(size, cap);
  427. }
  428. /*
  429. * User requested a write of security.capability. If needed, update the
  430. * xattr to change from v2 to v3, or to fixup the v3 rootid.
  431. *
  432. * If all is ok, we return the new size, on error return < 0.
  433. */
  434. int cap_convert_nscap(struct dentry *dentry, void **ivalue, size_t size)
  435. {
  436. struct vfs_ns_cap_data *nscap;
  437. uid_t nsrootid;
  438. const struct vfs_cap_data *cap = *ivalue;
  439. __u32 magic, nsmagic;
  440. struct inode *inode = d_backing_inode(dentry);
  441. struct user_namespace *task_ns = current_user_ns(),
  442. *fs_ns = inode->i_sb->s_user_ns;
  443. kuid_t rootid;
  444. size_t newsize;
  445. if (!*ivalue)
  446. return -EINVAL;
  447. if (!validheader(size, cap))
  448. return -EINVAL;
  449. if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
  450. return -EPERM;
  451. if (size == XATTR_CAPS_SZ_2)
  452. if (ns_capable(inode->i_sb->s_user_ns, CAP_SETFCAP))
  453. /* user is privileged, just write the v2 */
  454. return size;
  455. rootid = rootid_from_xattr(*ivalue, size, task_ns);
  456. if (!uid_valid(rootid))
  457. return -EINVAL;
  458. nsrootid = from_kuid(fs_ns, rootid);
  459. if (nsrootid == -1)
  460. return -EINVAL;
  461. newsize = sizeof(struct vfs_ns_cap_data);
  462. nscap = kmalloc(newsize, GFP_ATOMIC);
  463. if (!nscap)
  464. return -ENOMEM;
  465. nscap->rootid = cpu_to_le32(nsrootid);
  466. nsmagic = VFS_CAP_REVISION_3;
  467. magic = le32_to_cpu(cap->magic_etc);
  468. if (magic & VFS_CAP_FLAGS_EFFECTIVE)
  469. nsmagic |= VFS_CAP_FLAGS_EFFECTIVE;
  470. nscap->magic_etc = cpu_to_le32(nsmagic);
  471. memcpy(&nscap->data, &cap->data, sizeof(__le32) * 2 * VFS_CAP_U32);
  472. kvfree(*ivalue);
  473. *ivalue = nscap;
  474. return newsize;
  475. }
  476. /*
  477. * Calculate the new process capability sets from the capability sets attached
  478. * to a file.
  479. */
  480. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  481. struct linux_binprm *bprm,
  482. bool *effective,
  483. bool *has_fcap)
  484. {
  485. struct cred *new = bprm->cred;
  486. unsigned i;
  487. int ret = 0;
  488. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  489. *effective = true;
  490. if (caps->magic_etc & VFS_CAP_REVISION_MASK)
  491. *has_fcap = true;
  492. CAP_FOR_EACH_U32(i) {
  493. __u32 permitted = caps->permitted.cap[i];
  494. __u32 inheritable = caps->inheritable.cap[i];
  495. /*
  496. * pP' = (X & fP) | (pI & fI)
  497. * The addition of pA' is handled later.
  498. */
  499. new->cap_permitted.cap[i] =
  500. (new->cap_bset.cap[i] & permitted) |
  501. (new->cap_inheritable.cap[i] & inheritable);
  502. if (permitted & ~new->cap_permitted.cap[i])
  503. /* insufficient to execute correctly */
  504. ret = -EPERM;
  505. }
  506. /*
  507. * For legacy apps, with no internal support for recognizing they
  508. * do not have enough capabilities, we return an error if they are
  509. * missing some "forced" (aka file-permitted) capabilities.
  510. */
  511. return *effective ? ret : 0;
  512. }
  513. /*
  514. * Extract the on-exec-apply capability sets for an executable file.
  515. */
  516. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  517. {
  518. struct inode *inode = d_backing_inode(dentry);
  519. __u32 magic_etc;
  520. unsigned tocopy, i;
  521. int size;
  522. struct vfs_ns_cap_data data, *nscaps = &data;
  523. struct vfs_cap_data *caps = (struct vfs_cap_data *) &data;
  524. kuid_t rootkuid;
  525. struct user_namespace *fs_ns;
  526. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  527. if (!inode)
  528. return -ENODATA;
  529. fs_ns = inode->i_sb->s_user_ns;
  530. size = __vfs_getxattr((struct dentry *)dentry, inode,
  531. XATTR_NAME_CAPS, &data, XATTR_CAPS_SZ);
  532. if (size == -ENODATA || size == -EOPNOTSUPP)
  533. /* no data, that's ok */
  534. return -ENODATA;
  535. if (size < 0)
  536. return size;
  537. if (size < sizeof(magic_etc))
  538. return -EINVAL;
  539. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps->magic_etc);
  540. rootkuid = make_kuid(fs_ns, 0);
  541. switch (magic_etc & VFS_CAP_REVISION_MASK) {
  542. case VFS_CAP_REVISION_1:
  543. if (size != XATTR_CAPS_SZ_1)
  544. return -EINVAL;
  545. tocopy = VFS_CAP_U32_1;
  546. break;
  547. case VFS_CAP_REVISION_2:
  548. if (size != XATTR_CAPS_SZ_2)
  549. return -EINVAL;
  550. tocopy = VFS_CAP_U32_2;
  551. break;
  552. case VFS_CAP_REVISION_3:
  553. if (size != XATTR_CAPS_SZ_3)
  554. return -EINVAL;
  555. tocopy = VFS_CAP_U32_3;
  556. rootkuid = make_kuid(fs_ns, le32_to_cpu(nscaps->rootid));
  557. break;
  558. default:
  559. return -EINVAL;
  560. }
  561. /* Limit the caps to the mounter of the filesystem
  562. * or the more limited uid specified in the xattr.
  563. */
  564. if (!rootid_owns_currentns(rootkuid))
  565. return -ENODATA;
  566. CAP_FOR_EACH_U32(i) {
  567. if (i >= tocopy)
  568. break;
  569. cpu_caps->permitted.cap[i] = le32_to_cpu(caps->data[i].permitted);
  570. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps->data[i].inheritable);
  571. }
  572. cpu_caps->permitted.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  573. cpu_caps->inheritable.cap[CAP_LAST_U32] &= CAP_LAST_U32_VALID_MASK;
  574. return 0;
  575. }
  576. /*
  577. * Attempt to get the on-exec apply capability sets for an executable file from
  578. * its xattrs and, if present, apply them to the proposed credentials being
  579. * constructed by execve().
  580. */
  581. static int get_file_caps(struct linux_binprm *bprm, bool *effective, bool *has_fcap)
  582. {
  583. int rc = 0;
  584. struct cpu_vfs_cap_data vcaps;
  585. cap_clear(bprm->cred->cap_permitted);
  586. if (!file_caps_enabled)
  587. return 0;
  588. if (!mnt_may_suid(bprm->file->f_path.mnt))
  589. return 0;
  590. /*
  591. * This check is redundant with mnt_may_suid() but is kept to make
  592. * explicit that capability bits are limited to s_user_ns and its
  593. * descendants.
  594. */
  595. if (!current_in_userns(bprm->file->f_path.mnt->mnt_sb->s_user_ns))
  596. return 0;
  597. rc = get_vfs_caps_from_disk(bprm->file->f_path.dentry, &vcaps);
  598. if (rc < 0) {
  599. if (rc == -EINVAL)
  600. printk(KERN_NOTICE "Invalid argument reading file caps for %s\n",
  601. bprm->filename);
  602. else if (rc == -ENODATA)
  603. rc = 0;
  604. goto out;
  605. }
  606. rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective, has_fcap);
  607. if (rc == -EINVAL)
  608. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  609. __func__, rc, bprm->filename);
  610. out:
  611. if (rc)
  612. cap_clear(bprm->cred->cap_permitted);
  613. return rc;
  614. }
  615. static inline bool root_privileged(void) { return !issecure(SECURE_NOROOT); }
  616. static inline bool __is_real(kuid_t uid, struct cred *cred)
  617. { return uid_eq(cred->uid, uid); }
  618. static inline bool __is_eff(kuid_t uid, struct cred *cred)
  619. { return uid_eq(cred->euid, uid); }
  620. static inline bool __is_suid(kuid_t uid, struct cred *cred)
  621. { return !__is_real(uid, cred) && __is_eff(uid, cred); }
  622. /*
  623. * handle_privileged_root - Handle case of privileged root
  624. * @bprm: The execution parameters, including the proposed creds
  625. * @has_fcap: Are any file capabilities set?
  626. * @effective: Do we have effective root privilege?
  627. * @root_uid: This namespace' root UID WRT initial USER namespace
  628. *
  629. * Handle the case where root is privileged and hasn't been neutered by
  630. * SECURE_NOROOT. If file capabilities are set, they won't be combined with
  631. * set UID root and nothing is changed. If we are root, cap_permitted is
  632. * updated. If we have become set UID root, the effective bit is set.
  633. */
  634. static void handle_privileged_root(struct linux_binprm *bprm, bool has_fcap,
  635. bool *effective, kuid_t root_uid)
  636. {
  637. const struct cred *old = current_cred();
  638. struct cred *new = bprm->cred;
  639. if (!root_privileged())
  640. return;
  641. /*
  642. * If the legacy file capability is set, then don't set privs
  643. * for a setuid root binary run by a non-root user. Do set it
  644. * for a root user just to cause least surprise to an admin.
  645. */
  646. if (has_fcap && __is_suid(root_uid, new)) {
  647. warn_setuid_and_fcaps_mixed(bprm->filename);
  648. return;
  649. }
  650. /*
  651. * To support inheritance of root-permissions and suid-root
  652. * executables under compatibility mode, we override the
  653. * capability sets for the file.
  654. */
  655. if (__is_eff(root_uid, new) || __is_real(root_uid, new)) {
  656. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  657. new->cap_permitted = cap_combine(old->cap_bset,
  658. old->cap_inheritable);
  659. }
  660. /*
  661. * If only the real uid is 0, we do not set the effective bit.
  662. */
  663. if (__is_eff(root_uid, new))
  664. *effective = true;
  665. }
  666. #define __cap_gained(field, target, source) \
  667. !cap_issubset(target->cap_##field, source->cap_##field)
  668. #define __cap_grew(target, source, cred) \
  669. !cap_issubset(cred->cap_##target, cred->cap_##source)
  670. #define __cap_full(field, cred) \
  671. cap_issubset(CAP_FULL_SET, cred->cap_##field)
  672. static inline bool __is_setuid(struct cred *new, const struct cred *old)
  673. { return !uid_eq(new->euid, old->uid); }
  674. static inline bool __is_setgid(struct cred *new, const struct cred *old)
  675. { return !gid_eq(new->egid, old->gid); }
  676. /*
  677. * 1) Audit candidate if current->cap_effective is set
  678. *
  679. * We do not bother to audit if 3 things are true:
  680. * 1) cap_effective has all caps
  681. * 2) we became root *OR* are were already root
  682. * 3) root is supposed to have all caps (SECURE_NOROOT)
  683. * Since this is just a normal root execing a process.
  684. *
  685. * Number 1 above might fail if you don't have a full bset, but I think
  686. * that is interesting information to audit.
  687. *
  688. * A number of other conditions require logging:
  689. * 2) something prevented setuid root getting all caps
  690. * 3) non-setuid root gets fcaps
  691. * 4) non-setuid root gets ambient
  692. */
  693. static inline bool nonroot_raised_pE(struct cred *new, const struct cred *old,
  694. kuid_t root, bool has_fcap)
  695. {
  696. bool ret = false;
  697. if ((__cap_grew(effective, ambient, new) &&
  698. !(__cap_full(effective, new) &&
  699. (__is_eff(root, new) || __is_real(root, new)) &&
  700. root_privileged())) ||
  701. (root_privileged() &&
  702. __is_suid(root, new) &&
  703. !__cap_full(effective, new)) ||
  704. (!__is_setuid(new, old) &&
  705. ((has_fcap &&
  706. __cap_gained(permitted, new, old)) ||
  707. __cap_gained(ambient, new, old))))
  708. ret = true;
  709. return ret;
  710. }
  711. /**
  712. * cap_bprm_set_creds - Set up the proposed credentials for execve().
  713. * @bprm: The execution parameters, including the proposed creds
  714. *
  715. * Set up the proposed credentials for a new execution context being
  716. * constructed by execve(). The proposed creds in @bprm->cred is altered,
  717. * which won't take effect immediately. Returns 0 if successful, -ve on error.
  718. */
  719. int cap_bprm_set_creds(struct linux_binprm *bprm)
  720. {
  721. const struct cred *old = current_cred();
  722. struct cred *new = bprm->cred;
  723. bool effective = false, has_fcap = false, is_setid;
  724. int ret;
  725. kuid_t root_uid;
  726. if (WARN_ON(!cap_ambient_invariant_ok(old)))
  727. return -EPERM;
  728. ret = get_file_caps(bprm, &effective, &has_fcap);
  729. if (ret < 0)
  730. return ret;
  731. root_uid = make_kuid(new->user_ns, 0);
  732. handle_privileged_root(bprm, has_fcap, &effective, root_uid);
  733. /* if we have fs caps, clear dangerous personality flags */
  734. if (__cap_gained(permitted, new, old))
  735. bprm->per_clear |= PER_CLEAR_ON_SETID;
  736. /* Don't let someone trace a set[ug]id/setpcap binary with the revised
  737. * credentials unless they have the appropriate permit.
  738. *
  739. * In addition, if NO_NEW_PRIVS, then ensure we get no new privs.
  740. */
  741. is_setid = __is_setuid(new, old) || __is_setgid(new, old);
  742. if ((is_setid || __cap_gained(permitted, new, old)) &&
  743. ((bprm->unsafe & ~LSM_UNSAFE_PTRACE) ||
  744. !ptracer_capable(current, new->user_ns))) {
  745. /* downgrade; they get no more than they had, and maybe less */
  746. if (!ns_capable(new->user_ns, CAP_SETUID) ||
  747. (bprm->unsafe & LSM_UNSAFE_NO_NEW_PRIVS)) {
  748. new->euid = new->uid;
  749. new->egid = new->gid;
  750. }
  751. new->cap_permitted = cap_intersect(new->cap_permitted,
  752. old->cap_permitted);
  753. }
  754. new->suid = new->fsuid = new->euid;
  755. new->sgid = new->fsgid = new->egid;
  756. /* File caps or setid cancels ambient. */
  757. if (has_fcap || is_setid)
  758. cap_clear(new->cap_ambient);
  759. /*
  760. * Now that we've computed pA', update pP' to give:
  761. * pP' = (X & fP) | (pI & fI) | pA'
  762. */
  763. new->cap_permitted = cap_combine(new->cap_permitted, new->cap_ambient);
  764. /*
  765. * Set pE' = (fE ? pP' : pA'). Because pA' is zero if fE is set,
  766. * this is the same as pE' = (fE ? pP' : 0) | pA'.
  767. */
  768. if (effective)
  769. new->cap_effective = new->cap_permitted;
  770. else
  771. new->cap_effective = new->cap_ambient;
  772. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  773. return -EPERM;
  774. if (nonroot_raised_pE(new, old, root_uid, has_fcap)) {
  775. ret = audit_log_bprm_fcaps(bprm, new, old);
  776. if (ret < 0)
  777. return ret;
  778. }
  779. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  780. if (WARN_ON(!cap_ambient_invariant_ok(new)))
  781. return -EPERM;
  782. /* Check for privilege-elevated exec. */
  783. bprm->cap_elevated = 0;
  784. if (is_setid ||
  785. (!__is_real(root_uid, new) &&
  786. (effective ||
  787. __cap_grew(permitted, ambient, new))))
  788. bprm->cap_elevated = 1;
  789. return 0;
  790. }
  791. /**
  792. * cap_inode_setxattr - Determine whether an xattr may be altered
  793. * @dentry: The inode/dentry being altered
  794. * @name: The name of the xattr to be changed
  795. * @value: The value that the xattr will be changed to
  796. * @size: The size of value
  797. * @flags: The replacement flag
  798. *
  799. * Determine whether an xattr may be altered or set on an inode, returning 0 if
  800. * permission is granted, -ve if denied.
  801. *
  802. * This is used to make sure security xattrs don't get updated or set by those
  803. * who aren't privileged to do so.
  804. */
  805. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  806. const void *value, size_t size, int flags)
  807. {
  808. struct user_namespace *user_ns = dentry->d_sb->s_user_ns;
  809. /* Ignore non-security xattrs */
  810. if (strncmp(name, XATTR_SECURITY_PREFIX,
  811. sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
  812. return 0;
  813. /*
  814. * For XATTR_NAME_CAPS the check will be done in
  815. * cap_convert_nscap(), called by setxattr()
  816. */
  817. if (strcmp(name, XATTR_NAME_CAPS) == 0)
  818. return 0;
  819. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  820. return -EPERM;
  821. return 0;
  822. }
  823. /**
  824. * cap_inode_removexattr - Determine whether an xattr may be removed
  825. * @dentry: The inode/dentry being altered
  826. * @name: The name of the xattr to be changed
  827. *
  828. * Determine whether an xattr may be removed from an inode, returning 0 if
  829. * permission is granted, -ve if denied.
  830. *
  831. * This is used to make sure security xattrs don't get removed by those who
  832. * aren't privileged to remove them.
  833. */
  834. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  835. {
  836. struct user_namespace *user_ns = dentry->d_sb->s_user_ns;
  837. /* Ignore non-security xattrs */
  838. if (strncmp(name, XATTR_SECURITY_PREFIX,
  839. sizeof(XATTR_SECURITY_PREFIX) - 1) != 0)
  840. return 0;
  841. if (strcmp(name, XATTR_NAME_CAPS) == 0) {
  842. /* security.capability gets namespaced */
  843. struct inode *inode = d_backing_inode(dentry);
  844. if (!inode)
  845. return -EINVAL;
  846. if (!capable_wrt_inode_uidgid(inode, CAP_SETFCAP))
  847. return -EPERM;
  848. return 0;
  849. }
  850. if (!ns_capable(user_ns, CAP_SYS_ADMIN))
  851. return -EPERM;
  852. return 0;
  853. }
  854. /*
  855. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  856. * a process after a call to setuid, setreuid, or setresuid.
  857. *
  858. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  859. * {r,e,s}uid != 0, the permitted and effective capabilities are
  860. * cleared.
  861. *
  862. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  863. * capabilities of the process are cleared.
  864. *
  865. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  866. * capabilities are set to the permitted capabilities.
  867. *
  868. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  869. * never happen.
  870. *
  871. * -astor
  872. *
  873. * cevans - New behaviour, Oct '99
  874. * A process may, via prctl(), elect to keep its capabilities when it
  875. * calls setuid() and switches away from uid==0. Both permitted and
  876. * effective sets will be retained.
  877. * Without this change, it was impossible for a daemon to drop only some
  878. * of its privilege. The call to setuid(!=0) would drop all privileges!
  879. * Keeping uid 0 is not an option because uid 0 owns too many vital
  880. * files..
  881. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  882. */
  883. static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
  884. {
  885. kuid_t root_uid = make_kuid(old->user_ns, 0);
  886. if ((uid_eq(old->uid, root_uid) ||
  887. uid_eq(old->euid, root_uid) ||
  888. uid_eq(old->suid, root_uid)) &&
  889. (!uid_eq(new->uid, root_uid) &&
  890. !uid_eq(new->euid, root_uid) &&
  891. !uid_eq(new->suid, root_uid))) {
  892. if (!issecure(SECURE_KEEP_CAPS)) {
  893. cap_clear(new->cap_permitted);
  894. cap_clear(new->cap_effective);
  895. }
  896. /*
  897. * Pre-ambient programs expect setresuid to nonroot followed
  898. * by exec to drop capabilities. We should make sure that
  899. * this remains the case.
  900. */
  901. cap_clear(new->cap_ambient);
  902. }
  903. if (uid_eq(old->euid, root_uid) && !uid_eq(new->euid, root_uid))
  904. cap_clear(new->cap_effective);
  905. if (!uid_eq(old->euid, root_uid) && uid_eq(new->euid, root_uid))
  906. new->cap_effective = new->cap_permitted;
  907. }
  908. /**
  909. * cap_task_fix_setuid - Fix up the results of setuid() call
  910. * @new: The proposed credentials
  911. * @old: The current task's current credentials
  912. * @flags: Indications of what has changed
  913. *
  914. * Fix up the results of setuid() call before the credential changes are
  915. * actually applied, returning 0 to grant the changes, -ve to deny them.
  916. */
  917. int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
  918. {
  919. switch (flags) {
  920. case LSM_SETID_RE:
  921. case LSM_SETID_ID:
  922. case LSM_SETID_RES:
  923. /* juggle the capabilities to follow [RES]UID changes unless
  924. * otherwise suppressed */
  925. if (!issecure(SECURE_NO_SETUID_FIXUP))
  926. cap_emulate_setxuid(new, old);
  927. break;
  928. case LSM_SETID_FS:
  929. /* juggle the capabilties to follow FSUID changes, unless
  930. * otherwise suppressed
  931. *
  932. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  933. * if not, we might be a bit too harsh here.
  934. */
  935. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  936. kuid_t root_uid = make_kuid(old->user_ns, 0);
  937. if (uid_eq(old->fsuid, root_uid) && !uid_eq(new->fsuid, root_uid))
  938. new->cap_effective =
  939. cap_drop_fs_set(new->cap_effective);
  940. if (!uid_eq(old->fsuid, root_uid) && uid_eq(new->fsuid, root_uid))
  941. new->cap_effective =
  942. cap_raise_fs_set(new->cap_effective,
  943. new->cap_permitted);
  944. }
  945. break;
  946. default:
  947. return -EINVAL;
  948. }
  949. return 0;
  950. }
  951. /*
  952. * Rationale: code calling task_setscheduler, task_setioprio, and
  953. * task_setnice, assumes that
  954. * . if capable(cap_sys_nice), then those actions should be allowed
  955. * . if not capable(cap_sys_nice), but acting on your own processes,
  956. * then those actions should be allowed
  957. * This is insufficient now since you can call code without suid, but
  958. * yet with increased caps.
  959. * So we check for increased caps on the target process.
  960. */
  961. static int cap_safe_nice(struct task_struct *p)
  962. {
  963. int is_subset, ret = 0;
  964. rcu_read_lock();
  965. is_subset = cap_issubset(__task_cred(p)->cap_permitted,
  966. current_cred()->cap_permitted);
  967. if (!is_subset && !ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE))
  968. ret = -EPERM;
  969. rcu_read_unlock();
  970. return ret;
  971. }
  972. /**
  973. * cap_task_setscheduler - Detemine if scheduler policy change is permitted
  974. * @p: The task to affect
  975. *
  976. * Detemine if the requested scheduler policy change is permitted for the
  977. * specified task, returning 0 if permission is granted, -ve if denied.
  978. */
  979. int cap_task_setscheduler(struct task_struct *p)
  980. {
  981. return cap_safe_nice(p);
  982. }
  983. /**
  984. * cap_task_ioprio - Detemine if I/O priority change is permitted
  985. * @p: The task to affect
  986. * @ioprio: The I/O priority to set
  987. *
  988. * Detemine if the requested I/O priority change is permitted for the specified
  989. * task, returning 0 if permission is granted, -ve if denied.
  990. */
  991. int cap_task_setioprio(struct task_struct *p, int ioprio)
  992. {
  993. return cap_safe_nice(p);
  994. }
  995. /**
  996. * cap_task_ioprio - Detemine if task priority change is permitted
  997. * @p: The task to affect
  998. * @nice: The nice value to set
  999. *
  1000. * Detemine if the requested task priority change is permitted for the
  1001. * specified task, returning 0 if permission is granted, -ve if denied.
  1002. */
  1003. int cap_task_setnice(struct task_struct *p, int nice)
  1004. {
  1005. return cap_safe_nice(p);
  1006. }
  1007. /*
  1008. * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
  1009. * the current task's bounding set. Returns 0 on success, -ve on error.
  1010. */
  1011. static int cap_prctl_drop(unsigned long cap)
  1012. {
  1013. struct cred *new;
  1014. if (!ns_capable(current_user_ns(), CAP_SETPCAP))
  1015. return -EPERM;
  1016. if (!cap_valid(cap))
  1017. return -EINVAL;
  1018. new = prepare_creds();
  1019. if (!new)
  1020. return -ENOMEM;
  1021. cap_lower(new->cap_bset, cap);
  1022. return commit_creds(new);
  1023. }
  1024. /**
  1025. * cap_task_prctl - Implement process control functions for this security module
  1026. * @option: The process control function requested
  1027. * @arg2, @arg3, @arg4, @arg5: The argument data for this function
  1028. *
  1029. * Allow process control functions (sys_prctl()) to alter capabilities; may
  1030. * also deny access to other functions not otherwise implemented here.
  1031. *
  1032. * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
  1033. * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
  1034. * modules will consider performing the function.
  1035. */
  1036. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  1037. unsigned long arg4, unsigned long arg5)
  1038. {
  1039. const struct cred *old = current_cred();
  1040. struct cred *new;
  1041. switch (option) {
  1042. case PR_CAPBSET_READ:
  1043. if (!cap_valid(arg2))
  1044. return -EINVAL;
  1045. return !!cap_raised(old->cap_bset, arg2);
  1046. case PR_CAPBSET_DROP:
  1047. return cap_prctl_drop(arg2);
  1048. /*
  1049. * The next four prctl's remain to assist with transitioning a
  1050. * system from legacy UID=0 based privilege (when filesystem
  1051. * capabilities are not in use) to a system using filesystem
  1052. * capabilities only - as the POSIX.1e draft intended.
  1053. *
  1054. * Note:
  1055. *
  1056. * PR_SET_SECUREBITS =
  1057. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  1058. * | issecure_mask(SECURE_NOROOT)
  1059. * | issecure_mask(SECURE_NOROOT_LOCKED)
  1060. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  1061. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  1062. *
  1063. * will ensure that the current process and all of its
  1064. * children will be locked into a pure
  1065. * capability-based-privilege environment.
  1066. */
  1067. case PR_SET_SECUREBITS:
  1068. if ((((old->securebits & SECURE_ALL_LOCKS) >> 1)
  1069. & (old->securebits ^ arg2)) /*[1]*/
  1070. || ((old->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
  1071. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  1072. || (cap_capable(current_cred(),
  1073. current_cred()->user_ns, CAP_SETPCAP,
  1074. SECURITY_CAP_AUDIT) != 0) /*[4]*/
  1075. /*
  1076. * [1] no changing of bits that are locked
  1077. * [2] no unlocking of locks
  1078. * [3] no setting of unsupported bits
  1079. * [4] doing anything requires privilege (go read about
  1080. * the "sendmail capabilities bug")
  1081. */
  1082. )
  1083. /* cannot change a locked bit */
  1084. return -EPERM;
  1085. new = prepare_creds();
  1086. if (!new)
  1087. return -ENOMEM;
  1088. new->securebits = arg2;
  1089. return commit_creds(new);
  1090. case PR_GET_SECUREBITS:
  1091. return old->securebits;
  1092. case PR_GET_KEEPCAPS:
  1093. return !!issecure(SECURE_KEEP_CAPS);
  1094. case PR_SET_KEEPCAPS:
  1095. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  1096. return -EINVAL;
  1097. if (issecure(SECURE_KEEP_CAPS_LOCKED))
  1098. return -EPERM;
  1099. new = prepare_creds();
  1100. if (!new)
  1101. return -ENOMEM;
  1102. if (arg2)
  1103. new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  1104. else
  1105. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  1106. return commit_creds(new);
  1107. case PR_CAP_AMBIENT:
  1108. if (arg2 == PR_CAP_AMBIENT_CLEAR_ALL) {
  1109. if (arg3 | arg4 | arg5)
  1110. return -EINVAL;
  1111. new = prepare_creds();
  1112. if (!new)
  1113. return -ENOMEM;
  1114. cap_clear(new->cap_ambient);
  1115. return commit_creds(new);
  1116. }
  1117. if (((!cap_valid(arg3)) | arg4 | arg5))
  1118. return -EINVAL;
  1119. if (arg2 == PR_CAP_AMBIENT_IS_SET) {
  1120. return !!cap_raised(current_cred()->cap_ambient, arg3);
  1121. } else if (arg2 != PR_CAP_AMBIENT_RAISE &&
  1122. arg2 != PR_CAP_AMBIENT_LOWER) {
  1123. return -EINVAL;
  1124. } else {
  1125. if (arg2 == PR_CAP_AMBIENT_RAISE &&
  1126. (!cap_raised(current_cred()->cap_permitted, arg3) ||
  1127. !cap_raised(current_cred()->cap_inheritable,
  1128. arg3) ||
  1129. issecure(SECURE_NO_CAP_AMBIENT_RAISE)))
  1130. return -EPERM;
  1131. new = prepare_creds();
  1132. if (!new)
  1133. return -ENOMEM;
  1134. if (arg2 == PR_CAP_AMBIENT_RAISE)
  1135. cap_raise(new->cap_ambient, arg3);
  1136. else
  1137. cap_lower(new->cap_ambient, arg3);
  1138. return commit_creds(new);
  1139. }
  1140. default:
  1141. /* No functionality available - continue with default */
  1142. return -ENOSYS;
  1143. }
  1144. }
  1145. /**
  1146. * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
  1147. * @mm: The VM space in which the new mapping is to be made
  1148. * @pages: The size of the mapping
  1149. *
  1150. * Determine whether the allocation of a new virtual mapping by the current
  1151. * task is permitted, returning 1 if permission is granted, 0 if not.
  1152. */
  1153. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  1154. {
  1155. int cap_sys_admin = 0;
  1156. if (cap_capable(current_cred(), &init_user_ns, CAP_SYS_ADMIN,
  1157. SECURITY_CAP_NOAUDIT) == 0)
  1158. cap_sys_admin = 1;
  1159. return cap_sys_admin;
  1160. }
  1161. /*
  1162. * cap_mmap_addr - check if able to map given addr
  1163. * @addr: address attempting to be mapped
  1164. *
  1165. * If the process is attempting to map memory below dac_mmap_min_addr they need
  1166. * CAP_SYS_RAWIO. The other parameters to this function are unused by the
  1167. * capability security module. Returns 0 if this mapping should be allowed
  1168. * -EPERM if not.
  1169. */
  1170. int cap_mmap_addr(unsigned long addr)
  1171. {
  1172. int ret = 0;
  1173. if (addr < dac_mmap_min_addr) {
  1174. ret = cap_capable(current_cred(), &init_user_ns, CAP_SYS_RAWIO,
  1175. SECURITY_CAP_AUDIT);
  1176. /* set PF_SUPERPRIV if it turns out we allow the low mmap */
  1177. if (ret == 0)
  1178. current->flags |= PF_SUPERPRIV;
  1179. }
  1180. return ret;
  1181. }
  1182. int cap_mmap_file(struct file *file, unsigned long reqprot,
  1183. unsigned long prot, unsigned long flags)
  1184. {
  1185. return 0;
  1186. }
  1187. #ifdef CONFIG_SECURITY
  1188. struct security_hook_list capability_hooks[] __lsm_ro_after_init = {
  1189. LSM_HOOK_INIT(capable, cap_capable),
  1190. LSM_HOOK_INIT(settime, cap_settime),
  1191. LSM_HOOK_INIT(ptrace_access_check, cap_ptrace_access_check),
  1192. LSM_HOOK_INIT(ptrace_traceme, cap_ptrace_traceme),
  1193. LSM_HOOK_INIT(capget, cap_capget),
  1194. LSM_HOOK_INIT(capset, cap_capset),
  1195. LSM_HOOK_INIT(bprm_set_creds, cap_bprm_set_creds),
  1196. LSM_HOOK_INIT(inode_need_killpriv, cap_inode_need_killpriv),
  1197. LSM_HOOK_INIT(inode_killpriv, cap_inode_killpriv),
  1198. LSM_HOOK_INIT(inode_getsecurity, cap_inode_getsecurity),
  1199. LSM_HOOK_INIT(mmap_addr, cap_mmap_addr),
  1200. LSM_HOOK_INIT(mmap_file, cap_mmap_file),
  1201. LSM_HOOK_INIT(task_fix_setuid, cap_task_fix_setuid),
  1202. LSM_HOOK_INIT(task_prctl, cap_task_prctl),
  1203. LSM_HOOK_INIT(task_setscheduler, cap_task_setscheduler),
  1204. LSM_HOOK_INIT(task_setioprio, cap_task_setioprio),
  1205. LSM_HOOK_INIT(task_setnice, cap_task_setnice),
  1206. LSM_HOOK_INIT(vm_enough_memory, cap_vm_enough_memory),
  1207. };
  1208. void __init capability_add_hooks(void)
  1209. {
  1210. security_add_hooks(capability_hooks, ARRAY_SIZE(capability_hooks),
  1211. "capability");
  1212. }
  1213. #endif /* CONFIG_SECURITY */