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