commoncap.c 43 KB

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