commoncap.c 37 KB

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