commoncap.c 40 KB

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