commoncap.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  1. /* Common capabilities, needed by capability.o and root_plug.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/security.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. /*
  30. * If a non-root user executes a setuid-root binary in
  31. * !secure(SECURE_NOROOT) mode, then we raise capabilities.
  32. * However if fE is also set, then the intent is for only
  33. * the file capabilities to be applied, and the setuid-root
  34. * bit is left on either to change the uid (plausible) or
  35. * to get full privilege on a kernel without file capabilities
  36. * support. So in that case we do not raise capabilities.
  37. *
  38. * Warn if that happens, once per boot.
  39. */
  40. static void warn_setuid_and_fcaps_mixed(char *fname)
  41. {
  42. static int warned;
  43. if (!warned) {
  44. printk(KERN_INFO "warning: `%s' has both setuid-root and"
  45. " effective capabilities. Therefore not raising all"
  46. " capabilities.\n", fname);
  47. warned = 1;
  48. }
  49. }
  50. int cap_netlink_send(struct sock *sk, struct sk_buff *skb)
  51. {
  52. NETLINK_CB(skb).eff_cap = current_cap();
  53. return 0;
  54. }
  55. int cap_netlink_recv(struct sk_buff *skb, int cap)
  56. {
  57. if (!cap_raised(NETLINK_CB(skb).eff_cap, cap))
  58. return -EPERM;
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(cap_netlink_recv);
  62. /**
  63. * cap_capable - Determine whether a task has a particular effective capability
  64. * @tsk: The task to query
  65. * @cred: The credentials to use
  66. * @cap: The capability to check for
  67. * @audit: Whether to write an audit message or not
  68. *
  69. * Determine whether the nominated task has the specified capability amongst
  70. * its effective set, returning 0 if it does, -ve if it does not.
  71. *
  72. * NOTE WELL: cap_has_capability() cannot be used like the kernel's capable()
  73. * and has_capability() functions. That is, it has the reverse semantics:
  74. * cap_has_capability() returns 0 when a task has a capability, but the
  75. * kernel's capable() and has_capability() returns 1 for this case.
  76. */
  77. int cap_capable(struct task_struct *tsk, const struct cred *cred, int cap,
  78. int audit)
  79. {
  80. return cap_raised(cred->cap_effective, cap) ? 0 : -EPERM;
  81. }
  82. /**
  83. * cap_settime - Determine whether the current process may set the system clock
  84. * @ts: The time to set
  85. * @tz: The timezone to set
  86. *
  87. * Determine whether the current process may set the system clock and timezone
  88. * information, returning 0 if permission granted, -ve if denied.
  89. */
  90. int cap_settime(struct timespec *ts, struct timezone *tz)
  91. {
  92. if (!capable(CAP_SYS_TIME))
  93. return -EPERM;
  94. return 0;
  95. }
  96. /**
  97. * cap_ptrace_access_check - Determine whether the current process may access
  98. * another
  99. * @child: The process to be accessed
  100. * @mode: The mode of attachment.
  101. *
  102. * Determine whether a process may access another, returning 0 if permission
  103. * granted, -ve if denied.
  104. */
  105. int cap_ptrace_access_check(struct task_struct *child, unsigned int mode)
  106. {
  107. int ret = 0;
  108. rcu_read_lock();
  109. if (!cap_issubset(__task_cred(child)->cap_permitted,
  110. current_cred()->cap_permitted) &&
  111. !capable(CAP_SYS_PTRACE))
  112. ret = -EPERM;
  113. rcu_read_unlock();
  114. return ret;
  115. }
  116. /**
  117. * cap_ptrace_traceme - Determine whether another process may trace the current
  118. * @parent: The task proposed to be the tracer
  119. *
  120. * Determine whether the nominated task is permitted to trace the current
  121. * process, returning 0 if permission is granted, -ve if denied.
  122. */
  123. int cap_ptrace_traceme(struct task_struct *parent)
  124. {
  125. int ret = 0;
  126. rcu_read_lock();
  127. if (!cap_issubset(current_cred()->cap_permitted,
  128. __task_cred(parent)->cap_permitted) &&
  129. !has_capability(parent, CAP_SYS_PTRACE))
  130. ret = -EPERM;
  131. rcu_read_unlock();
  132. return ret;
  133. }
  134. /**
  135. * cap_capget - Retrieve a task's capability sets
  136. * @target: The task from which to retrieve the capability sets
  137. * @effective: The place to record the effective set
  138. * @inheritable: The place to record the inheritable set
  139. * @permitted: The place to record the permitted set
  140. *
  141. * This function retrieves the capabilities of the nominated task and returns
  142. * them to the caller.
  143. */
  144. int cap_capget(struct task_struct *target, kernel_cap_t *effective,
  145. kernel_cap_t *inheritable, kernel_cap_t *permitted)
  146. {
  147. const struct cred *cred;
  148. /* Derived from kernel/capability.c:sys_capget. */
  149. rcu_read_lock();
  150. cred = __task_cred(target);
  151. *effective = cred->cap_effective;
  152. *inheritable = cred->cap_inheritable;
  153. *permitted = cred->cap_permitted;
  154. rcu_read_unlock();
  155. return 0;
  156. }
  157. /*
  158. * Determine whether the inheritable capabilities are limited to the old
  159. * permitted set. Returns 1 if they are limited, 0 if they are not.
  160. */
  161. static inline int cap_inh_is_capped(void)
  162. {
  163. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  164. /* they are so limited unless the current task has the CAP_SETPCAP
  165. * capability
  166. */
  167. if (cap_capable(current, current_cred(), CAP_SETPCAP,
  168. SECURITY_CAP_AUDIT) == 0)
  169. return 0;
  170. #endif
  171. return 1;
  172. }
  173. /**
  174. * cap_capset - Validate and apply proposed changes to current's capabilities
  175. * @new: The proposed new credentials; alterations should be made here
  176. * @old: The current task's current credentials
  177. * @effective: A pointer to the proposed new effective capabilities set
  178. * @inheritable: A pointer to the proposed new inheritable capabilities set
  179. * @permitted: A pointer to the proposed new permitted capabilities set
  180. *
  181. * This function validates and applies a proposed mass change to the current
  182. * process's capability sets. The changes are made to the proposed new
  183. * credentials, and assuming no error, will be committed by the caller of LSM.
  184. */
  185. int cap_capset(struct cred *new,
  186. const struct cred *old,
  187. const kernel_cap_t *effective,
  188. const kernel_cap_t *inheritable,
  189. const kernel_cap_t *permitted)
  190. {
  191. if (cap_inh_is_capped() &&
  192. !cap_issubset(*inheritable,
  193. cap_combine(old->cap_inheritable,
  194. old->cap_permitted)))
  195. /* incapable of using this inheritable set */
  196. return -EPERM;
  197. if (!cap_issubset(*inheritable,
  198. cap_combine(old->cap_inheritable,
  199. old->cap_bset)))
  200. /* no new pI capabilities outside bounding set */
  201. return -EPERM;
  202. /* verify restrictions on target's new Permitted set */
  203. if (!cap_issubset(*permitted, old->cap_permitted))
  204. return -EPERM;
  205. /* verify the _new_Effective_ is a subset of the _new_Permitted_ */
  206. if (!cap_issubset(*effective, *permitted))
  207. return -EPERM;
  208. new->cap_effective = *effective;
  209. new->cap_inheritable = *inheritable;
  210. new->cap_permitted = *permitted;
  211. return 0;
  212. }
  213. /*
  214. * Clear proposed capability sets for execve().
  215. */
  216. static inline void bprm_clear_caps(struct linux_binprm *bprm)
  217. {
  218. cap_clear(bprm->cred->cap_permitted);
  219. bprm->cap_effective = false;
  220. }
  221. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  222. /**
  223. * cap_inode_need_killpriv - Determine if inode change affects privileges
  224. * @dentry: The inode/dentry in being changed with change marked ATTR_KILL_PRIV
  225. *
  226. * Determine if an inode having a change applied that's marked ATTR_KILL_PRIV
  227. * affects the security markings on that inode, and if it is, should
  228. * inode_killpriv() be invoked or the change rejected?
  229. *
  230. * Returns 0 if granted; +ve if granted, but inode_killpriv() is required; and
  231. * -ve to deny the change.
  232. */
  233. int cap_inode_need_killpriv(struct dentry *dentry)
  234. {
  235. struct inode *inode = dentry->d_inode;
  236. int error;
  237. if (!inode->i_op->getxattr)
  238. return 0;
  239. error = inode->i_op->getxattr(dentry, XATTR_NAME_CAPS, NULL, 0);
  240. if (error <= 0)
  241. return 0;
  242. return 1;
  243. }
  244. /**
  245. * cap_inode_killpriv - Erase the security markings on an inode
  246. * @dentry: The inode/dentry to alter
  247. *
  248. * Erase the privilege-enhancing security markings on an inode.
  249. *
  250. * Returns 0 if successful, -ve on error.
  251. */
  252. int cap_inode_killpriv(struct dentry *dentry)
  253. {
  254. struct inode *inode = dentry->d_inode;
  255. if (!inode->i_op->removexattr)
  256. return 0;
  257. return inode->i_op->removexattr(dentry, XATTR_NAME_CAPS);
  258. }
  259. /*
  260. * Calculate the new process capability sets from the capability sets attached
  261. * to a file.
  262. */
  263. static inline int bprm_caps_from_vfs_caps(struct cpu_vfs_cap_data *caps,
  264. struct linux_binprm *bprm,
  265. bool *effective)
  266. {
  267. struct cred *new = bprm->cred;
  268. unsigned i;
  269. int ret = 0;
  270. if (caps->magic_etc & VFS_CAP_FLAGS_EFFECTIVE)
  271. *effective = true;
  272. CAP_FOR_EACH_U32(i) {
  273. __u32 permitted = caps->permitted.cap[i];
  274. __u32 inheritable = caps->inheritable.cap[i];
  275. /*
  276. * pP' = (X & fP) | (pI & fI)
  277. */
  278. new->cap_permitted.cap[i] =
  279. (new->cap_bset.cap[i] & permitted) |
  280. (new->cap_inheritable.cap[i] & inheritable);
  281. if (permitted & ~new->cap_permitted.cap[i])
  282. /* insufficient to execute correctly */
  283. ret = -EPERM;
  284. }
  285. /*
  286. * For legacy apps, with no internal support for recognizing they
  287. * do not have enough capabilities, we return an error if they are
  288. * missing some "forced" (aka file-permitted) capabilities.
  289. */
  290. return *effective ? ret : 0;
  291. }
  292. /*
  293. * Extract the on-exec-apply capability sets for an executable file.
  294. */
  295. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  296. {
  297. struct inode *inode = dentry->d_inode;
  298. __u32 magic_etc;
  299. unsigned tocopy, i;
  300. int size;
  301. struct vfs_cap_data caps;
  302. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  303. if (!inode || !inode->i_op->getxattr)
  304. return -ENODATA;
  305. size = inode->i_op->getxattr((struct dentry *)dentry, XATTR_NAME_CAPS, &caps,
  306. XATTR_CAPS_SZ);
  307. if (size == -ENODATA || size == -EOPNOTSUPP)
  308. /* no data, that's ok */
  309. return -ENODATA;
  310. if (size < 0)
  311. return size;
  312. if (size < sizeof(magic_etc))
  313. return -EINVAL;
  314. cpu_caps->magic_etc = magic_etc = le32_to_cpu(caps.magic_etc);
  315. switch (magic_etc & VFS_CAP_REVISION_MASK) {
  316. case VFS_CAP_REVISION_1:
  317. if (size != XATTR_CAPS_SZ_1)
  318. return -EINVAL;
  319. tocopy = VFS_CAP_U32_1;
  320. break;
  321. case VFS_CAP_REVISION_2:
  322. if (size != XATTR_CAPS_SZ_2)
  323. return -EINVAL;
  324. tocopy = VFS_CAP_U32_2;
  325. break;
  326. default:
  327. return -EINVAL;
  328. }
  329. CAP_FOR_EACH_U32(i) {
  330. if (i >= tocopy)
  331. break;
  332. cpu_caps->permitted.cap[i] = le32_to_cpu(caps.data[i].permitted);
  333. cpu_caps->inheritable.cap[i] = le32_to_cpu(caps.data[i].inheritable);
  334. }
  335. return 0;
  336. }
  337. /*
  338. * Attempt to get the on-exec apply capability sets for an executable file from
  339. * its xattrs and, if present, apply them to the proposed credentials being
  340. * constructed by execve().
  341. */
  342. static int get_file_caps(struct linux_binprm *bprm, bool *effective)
  343. {
  344. struct dentry *dentry;
  345. int rc = 0;
  346. struct cpu_vfs_cap_data vcaps;
  347. bprm_clear_caps(bprm);
  348. if (!file_caps_enabled)
  349. return 0;
  350. if (bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)
  351. return 0;
  352. dentry = dget(bprm->file->f_dentry);
  353. rc = get_vfs_caps_from_disk(dentry, &vcaps);
  354. if (rc < 0) {
  355. if (rc == -EINVAL)
  356. printk(KERN_NOTICE "%s: get_vfs_caps_from_disk returned %d for %s\n",
  357. __func__, rc, bprm->filename);
  358. else if (rc == -ENODATA)
  359. rc = 0;
  360. goto out;
  361. }
  362. rc = bprm_caps_from_vfs_caps(&vcaps, bprm, effective);
  363. if (rc == -EINVAL)
  364. printk(KERN_NOTICE "%s: cap_from_disk returned %d for %s\n",
  365. __func__, rc, bprm->filename);
  366. out:
  367. dput(dentry);
  368. if (rc)
  369. bprm_clear_caps(bprm);
  370. return rc;
  371. }
  372. #else
  373. int cap_inode_need_killpriv(struct dentry *dentry)
  374. {
  375. return 0;
  376. }
  377. int cap_inode_killpriv(struct dentry *dentry)
  378. {
  379. return 0;
  380. }
  381. int get_vfs_caps_from_disk(const struct dentry *dentry, struct cpu_vfs_cap_data *cpu_caps)
  382. {
  383. memset(cpu_caps, 0, sizeof(struct cpu_vfs_cap_data));
  384. return -ENODATA;
  385. }
  386. static inline int get_file_caps(struct linux_binprm *bprm, bool *effective)
  387. {
  388. bprm_clear_caps(bprm);
  389. return 0;
  390. }
  391. #endif
  392. /*
  393. * Determine whether a exec'ing process's new permitted capabilities should be
  394. * limited to just what it already has.
  395. *
  396. * This prevents processes that are being ptraced from gaining access to
  397. * CAP_SETPCAP, unless the process they're tracing already has it, and the
  398. * binary they're executing has filecaps that elevate it.
  399. *
  400. * Returns 1 if they should be limited, 0 if they are not.
  401. */
  402. static inline int cap_limit_ptraced_target(void)
  403. {
  404. #ifndef CONFIG_SECURITY_FILE_CAPABILITIES
  405. if (capable(CAP_SETPCAP))
  406. return 0;
  407. #endif
  408. return 1;
  409. }
  410. /**
  411. * cap_bprm_set_creds - Set up the proposed credentials for execve().
  412. * @bprm: The execution parameters, including the proposed creds
  413. *
  414. * Set up the proposed credentials for a new execution context being
  415. * constructed by execve(). The proposed creds in @bprm->cred is altered,
  416. * which won't take effect immediately. Returns 0 if successful, -ve on error.
  417. */
  418. int cap_bprm_set_creds(struct linux_binprm *bprm)
  419. {
  420. const struct cred *old = current_cred();
  421. struct cred *new = bprm->cred;
  422. bool effective;
  423. int ret;
  424. effective = false;
  425. ret = get_file_caps(bprm, &effective);
  426. if (ret < 0)
  427. return ret;
  428. if (!issecure(SECURE_NOROOT)) {
  429. /*
  430. * If the legacy file capability is set, then don't set privs
  431. * for a setuid root binary run by a non-root user. Do set it
  432. * for a root user just to cause least surprise to an admin.
  433. */
  434. if (effective && new->uid != 0 && new->euid == 0) {
  435. warn_setuid_and_fcaps_mixed(bprm->filename);
  436. goto skip;
  437. }
  438. /*
  439. * To support inheritance of root-permissions and suid-root
  440. * executables under compatibility mode, we override the
  441. * capability sets for the file.
  442. *
  443. * If only the real uid is 0, we do not set the effective bit.
  444. */
  445. if (new->euid == 0 || new->uid == 0) {
  446. /* pP' = (cap_bset & ~0) | (pI & ~0) */
  447. new->cap_permitted = cap_combine(old->cap_bset,
  448. old->cap_inheritable);
  449. }
  450. if (new->euid == 0)
  451. effective = true;
  452. }
  453. skip:
  454. /* Don't let someone trace a set[ug]id/setpcap binary with the revised
  455. * credentials unless they have the appropriate permit
  456. */
  457. if ((new->euid != old->uid ||
  458. new->egid != old->gid ||
  459. !cap_issubset(new->cap_permitted, old->cap_permitted)) &&
  460. bprm->unsafe & ~LSM_UNSAFE_PTRACE_CAP) {
  461. /* downgrade; they get no more than they had, and maybe less */
  462. if (!capable(CAP_SETUID)) {
  463. new->euid = new->uid;
  464. new->egid = new->gid;
  465. }
  466. if (cap_limit_ptraced_target())
  467. new->cap_permitted = cap_intersect(new->cap_permitted,
  468. old->cap_permitted);
  469. }
  470. new->suid = new->fsuid = new->euid;
  471. new->sgid = new->fsgid = new->egid;
  472. /* For init, we want to retain the capabilities set in the initial
  473. * task. Thus we skip the usual capability rules
  474. */
  475. if (!is_global_init(current)) {
  476. if (effective)
  477. new->cap_effective = new->cap_permitted;
  478. else
  479. cap_clear(new->cap_effective);
  480. }
  481. bprm->cap_effective = effective;
  482. /*
  483. * Audit candidate if current->cap_effective is set
  484. *
  485. * We do not bother to audit if 3 things are true:
  486. * 1) cap_effective has all caps
  487. * 2) we are root
  488. * 3) root is supposed to have all caps (SECURE_NOROOT)
  489. * Since this is just a normal root execing a process.
  490. *
  491. * Number 1 above might fail if you don't have a full bset, but I think
  492. * that is interesting information to audit.
  493. */
  494. if (!cap_isclear(new->cap_effective)) {
  495. if (!cap_issubset(CAP_FULL_SET, new->cap_effective) ||
  496. new->euid != 0 || new->uid != 0 ||
  497. issecure(SECURE_NOROOT)) {
  498. ret = audit_log_bprm_fcaps(bprm, new, old);
  499. if (ret < 0)
  500. return ret;
  501. }
  502. }
  503. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  504. return 0;
  505. }
  506. /**
  507. * cap_bprm_secureexec - Determine whether a secure execution is required
  508. * @bprm: The execution parameters
  509. *
  510. * Determine whether a secure execution is required, return 1 if it is, and 0
  511. * if it is not.
  512. *
  513. * The credentials have been committed by this point, and so are no longer
  514. * available through @bprm->cred.
  515. */
  516. int cap_bprm_secureexec(struct linux_binprm *bprm)
  517. {
  518. const struct cred *cred = current_cred();
  519. if (cred->uid != 0) {
  520. if (bprm->cap_effective)
  521. return 1;
  522. if (!cap_isclear(cred->cap_permitted))
  523. return 1;
  524. }
  525. return (cred->euid != cred->uid ||
  526. cred->egid != cred->gid);
  527. }
  528. /**
  529. * cap_inode_setxattr - Determine whether an xattr may be altered
  530. * @dentry: The inode/dentry being altered
  531. * @name: The name of the xattr to be changed
  532. * @value: The value that the xattr will be changed to
  533. * @size: The size of value
  534. * @flags: The replacement flag
  535. *
  536. * Determine whether an xattr may be altered or set on an inode, returning 0 if
  537. * permission is granted, -ve if denied.
  538. *
  539. * This is used to make sure security xattrs don't get updated or set by those
  540. * who aren't privileged to do so.
  541. */
  542. int cap_inode_setxattr(struct dentry *dentry, const char *name,
  543. const void *value, size_t size, int flags)
  544. {
  545. if (!strcmp(name, XATTR_NAME_CAPS)) {
  546. if (!capable(CAP_SETFCAP))
  547. return -EPERM;
  548. return 0;
  549. }
  550. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  551. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  552. !capable(CAP_SYS_ADMIN))
  553. return -EPERM;
  554. return 0;
  555. }
  556. /**
  557. * cap_inode_removexattr - Determine whether an xattr may be removed
  558. * @dentry: The inode/dentry being altered
  559. * @name: The name of the xattr to be changed
  560. *
  561. * Determine whether an xattr may be removed from an inode, returning 0 if
  562. * permission is granted, -ve if denied.
  563. *
  564. * This is used to make sure security xattrs don't get removed by those who
  565. * aren't privileged to remove them.
  566. */
  567. int cap_inode_removexattr(struct dentry *dentry, const char *name)
  568. {
  569. if (!strcmp(name, XATTR_NAME_CAPS)) {
  570. if (!capable(CAP_SETFCAP))
  571. return -EPERM;
  572. return 0;
  573. }
  574. if (!strncmp(name, XATTR_SECURITY_PREFIX,
  575. sizeof(XATTR_SECURITY_PREFIX) - 1) &&
  576. !capable(CAP_SYS_ADMIN))
  577. return -EPERM;
  578. return 0;
  579. }
  580. /*
  581. * cap_emulate_setxuid() fixes the effective / permitted capabilities of
  582. * a process after a call to setuid, setreuid, or setresuid.
  583. *
  584. * 1) When set*uiding _from_ one of {r,e,s}uid == 0 _to_ all of
  585. * {r,e,s}uid != 0, the permitted and effective capabilities are
  586. * cleared.
  587. *
  588. * 2) When set*uiding _from_ euid == 0 _to_ euid != 0, the effective
  589. * capabilities of the process are cleared.
  590. *
  591. * 3) When set*uiding _from_ euid != 0 _to_ euid == 0, the effective
  592. * capabilities are set to the permitted capabilities.
  593. *
  594. * fsuid is handled elsewhere. fsuid == 0 and {r,e,s}uid!= 0 should
  595. * never happen.
  596. *
  597. * -astor
  598. *
  599. * cevans - New behaviour, Oct '99
  600. * A process may, via prctl(), elect to keep its capabilities when it
  601. * calls setuid() and switches away from uid==0. Both permitted and
  602. * effective sets will be retained.
  603. * Without this change, it was impossible for a daemon to drop only some
  604. * of its privilege. The call to setuid(!=0) would drop all privileges!
  605. * Keeping uid 0 is not an option because uid 0 owns too many vital
  606. * files..
  607. * Thanks to Olaf Kirch and Peter Benie for spotting this.
  608. */
  609. static inline void cap_emulate_setxuid(struct cred *new, const struct cred *old)
  610. {
  611. if ((old->uid == 0 || old->euid == 0 || old->suid == 0) &&
  612. (new->uid != 0 && new->euid != 0 && new->suid != 0) &&
  613. !issecure(SECURE_KEEP_CAPS)) {
  614. cap_clear(new->cap_permitted);
  615. cap_clear(new->cap_effective);
  616. }
  617. if (old->euid == 0 && new->euid != 0)
  618. cap_clear(new->cap_effective);
  619. if (old->euid != 0 && new->euid == 0)
  620. new->cap_effective = new->cap_permitted;
  621. }
  622. /**
  623. * cap_task_fix_setuid - Fix up the results of setuid() call
  624. * @new: The proposed credentials
  625. * @old: The current task's current credentials
  626. * @flags: Indications of what has changed
  627. *
  628. * Fix up the results of setuid() call before the credential changes are
  629. * actually applied, returning 0 to grant the changes, -ve to deny them.
  630. */
  631. int cap_task_fix_setuid(struct cred *new, const struct cred *old, int flags)
  632. {
  633. switch (flags) {
  634. case LSM_SETID_RE:
  635. case LSM_SETID_ID:
  636. case LSM_SETID_RES:
  637. /* juggle the capabilities to follow [RES]UID changes unless
  638. * otherwise suppressed */
  639. if (!issecure(SECURE_NO_SETUID_FIXUP))
  640. cap_emulate_setxuid(new, old);
  641. break;
  642. case LSM_SETID_FS:
  643. /* juggle the capabilties to follow FSUID changes, unless
  644. * otherwise suppressed
  645. *
  646. * FIXME - is fsuser used for all CAP_FS_MASK capabilities?
  647. * if not, we might be a bit too harsh here.
  648. */
  649. if (!issecure(SECURE_NO_SETUID_FIXUP)) {
  650. if (old->fsuid == 0 && new->fsuid != 0)
  651. new->cap_effective =
  652. cap_drop_fs_set(new->cap_effective);
  653. if (old->fsuid != 0 && new->fsuid == 0)
  654. new->cap_effective =
  655. cap_raise_fs_set(new->cap_effective,
  656. new->cap_permitted);
  657. }
  658. break;
  659. default:
  660. return -EINVAL;
  661. }
  662. return 0;
  663. }
  664. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  665. /*
  666. * Rationale: code calling task_setscheduler, task_setioprio, and
  667. * task_setnice, assumes that
  668. * . if capable(cap_sys_nice), then those actions should be allowed
  669. * . if not capable(cap_sys_nice), but acting on your own processes,
  670. * then those actions should be allowed
  671. * This is insufficient now since you can call code without suid, but
  672. * yet with increased caps.
  673. * So we check for increased caps on the target process.
  674. */
  675. static int cap_safe_nice(struct task_struct *p)
  676. {
  677. int is_subset;
  678. rcu_read_lock();
  679. is_subset = cap_issubset(__task_cred(p)->cap_permitted,
  680. current_cred()->cap_permitted);
  681. rcu_read_unlock();
  682. if (!is_subset && !capable(CAP_SYS_NICE))
  683. return -EPERM;
  684. return 0;
  685. }
  686. /**
  687. * cap_task_setscheduler - Detemine if scheduler policy change is permitted
  688. * @p: The task to affect
  689. * @policy: The policy to effect
  690. * @lp: The parameters to the scheduling policy
  691. *
  692. * Detemine if the requested scheduler policy change is permitted for the
  693. * specified task, returning 0 if permission is granted, -ve if denied.
  694. */
  695. int cap_task_setscheduler(struct task_struct *p, int policy,
  696. struct sched_param *lp)
  697. {
  698. return cap_safe_nice(p);
  699. }
  700. /**
  701. * cap_task_ioprio - Detemine if I/O priority change is permitted
  702. * @p: The task to affect
  703. * @ioprio: The I/O priority to set
  704. *
  705. * Detemine if the requested I/O priority change is permitted for the specified
  706. * task, returning 0 if permission is granted, -ve if denied.
  707. */
  708. int cap_task_setioprio(struct task_struct *p, int ioprio)
  709. {
  710. return cap_safe_nice(p);
  711. }
  712. /**
  713. * cap_task_ioprio - Detemine if task priority change is permitted
  714. * @p: The task to affect
  715. * @nice: The nice value to set
  716. *
  717. * Detemine if the requested task priority change is permitted for the
  718. * specified task, returning 0 if permission is granted, -ve if denied.
  719. */
  720. int cap_task_setnice(struct task_struct *p, int nice)
  721. {
  722. return cap_safe_nice(p);
  723. }
  724. /*
  725. * Implement PR_CAPBSET_DROP. Attempt to remove the specified capability from
  726. * the current task's bounding set. Returns 0 on success, -ve on error.
  727. */
  728. static long cap_prctl_drop(struct cred *new, unsigned long cap)
  729. {
  730. if (!capable(CAP_SETPCAP))
  731. return -EPERM;
  732. if (!cap_valid(cap))
  733. return -EINVAL;
  734. cap_lower(new->cap_bset, cap);
  735. return 0;
  736. }
  737. #else
  738. int cap_task_setscheduler (struct task_struct *p, int policy,
  739. struct sched_param *lp)
  740. {
  741. return 0;
  742. }
  743. int cap_task_setioprio (struct task_struct *p, int ioprio)
  744. {
  745. return 0;
  746. }
  747. int cap_task_setnice (struct task_struct *p, int nice)
  748. {
  749. return 0;
  750. }
  751. #endif
  752. /**
  753. * cap_task_prctl - Implement process control functions for this security module
  754. * @option: The process control function requested
  755. * @arg2, @arg3, @arg4, @arg5: The argument data for this function
  756. *
  757. * Allow process control functions (sys_prctl()) to alter capabilities; may
  758. * also deny access to other functions not otherwise implemented here.
  759. *
  760. * Returns 0 or +ve on success, -ENOSYS if this function is not implemented
  761. * here, other -ve on error. If -ENOSYS is returned, sys_prctl() and other LSM
  762. * modules will consider performing the function.
  763. */
  764. int cap_task_prctl(int option, unsigned long arg2, unsigned long arg3,
  765. unsigned long arg4, unsigned long arg5)
  766. {
  767. struct cred *new;
  768. long error = 0;
  769. new = prepare_creds();
  770. if (!new)
  771. return -ENOMEM;
  772. switch (option) {
  773. case PR_CAPBSET_READ:
  774. error = -EINVAL;
  775. if (!cap_valid(arg2))
  776. goto error;
  777. error = !!cap_raised(new->cap_bset, arg2);
  778. goto no_change;
  779. #ifdef CONFIG_SECURITY_FILE_CAPABILITIES
  780. case PR_CAPBSET_DROP:
  781. error = cap_prctl_drop(new, arg2);
  782. if (error < 0)
  783. goto error;
  784. goto changed;
  785. /*
  786. * The next four prctl's remain to assist with transitioning a
  787. * system from legacy UID=0 based privilege (when filesystem
  788. * capabilities are not in use) to a system using filesystem
  789. * capabilities only - as the POSIX.1e draft intended.
  790. *
  791. * Note:
  792. *
  793. * PR_SET_SECUREBITS =
  794. * issecure_mask(SECURE_KEEP_CAPS_LOCKED)
  795. * | issecure_mask(SECURE_NOROOT)
  796. * | issecure_mask(SECURE_NOROOT_LOCKED)
  797. * | issecure_mask(SECURE_NO_SETUID_FIXUP)
  798. * | issecure_mask(SECURE_NO_SETUID_FIXUP_LOCKED)
  799. *
  800. * will ensure that the current process and all of its
  801. * children will be locked into a pure
  802. * capability-based-privilege environment.
  803. */
  804. case PR_SET_SECUREBITS:
  805. error = -EPERM;
  806. if ((((new->securebits & SECURE_ALL_LOCKS) >> 1)
  807. & (new->securebits ^ arg2)) /*[1]*/
  808. || ((new->securebits & SECURE_ALL_LOCKS & ~arg2)) /*[2]*/
  809. || (arg2 & ~(SECURE_ALL_LOCKS | SECURE_ALL_BITS)) /*[3]*/
  810. || (cap_capable(current, current_cred(), CAP_SETPCAP,
  811. SECURITY_CAP_AUDIT) != 0) /*[4]*/
  812. /*
  813. * [1] no changing of bits that are locked
  814. * [2] no unlocking of locks
  815. * [3] no setting of unsupported bits
  816. * [4] doing anything requires privilege (go read about
  817. * the "sendmail capabilities bug")
  818. */
  819. )
  820. /* cannot change a locked bit */
  821. goto error;
  822. new->securebits = arg2;
  823. goto changed;
  824. case PR_GET_SECUREBITS:
  825. error = new->securebits;
  826. goto no_change;
  827. #endif /* def CONFIG_SECURITY_FILE_CAPABILITIES */
  828. case PR_GET_KEEPCAPS:
  829. if (issecure(SECURE_KEEP_CAPS))
  830. error = 1;
  831. goto no_change;
  832. case PR_SET_KEEPCAPS:
  833. error = -EINVAL;
  834. if (arg2 > 1) /* Note, we rely on arg2 being unsigned here */
  835. goto error;
  836. error = -EPERM;
  837. if (issecure(SECURE_KEEP_CAPS_LOCKED))
  838. goto error;
  839. if (arg2)
  840. new->securebits |= issecure_mask(SECURE_KEEP_CAPS);
  841. else
  842. new->securebits &= ~issecure_mask(SECURE_KEEP_CAPS);
  843. goto changed;
  844. default:
  845. /* No functionality available - continue with default */
  846. error = -ENOSYS;
  847. goto error;
  848. }
  849. /* Functionality provided */
  850. changed:
  851. return commit_creds(new);
  852. no_change:
  853. error:
  854. abort_creds(new);
  855. return error;
  856. }
  857. /**
  858. * cap_syslog - Determine whether syslog function is permitted
  859. * @type: Function requested
  860. *
  861. * Determine whether the current process is permitted to use a particular
  862. * syslog function, returning 0 if permission is granted, -ve if not.
  863. */
  864. int cap_syslog(int type)
  865. {
  866. if ((type != 3 && type != 10) && !capable(CAP_SYS_ADMIN))
  867. return -EPERM;
  868. return 0;
  869. }
  870. /**
  871. * cap_vm_enough_memory - Determine whether a new virtual mapping is permitted
  872. * @mm: The VM space in which the new mapping is to be made
  873. * @pages: The size of the mapping
  874. *
  875. * Determine whether the allocation of a new virtual mapping by the current
  876. * task is permitted, returning 0 if permission is granted, -ve if not.
  877. */
  878. int cap_vm_enough_memory(struct mm_struct *mm, long pages)
  879. {
  880. int cap_sys_admin = 0;
  881. if (cap_capable(current, current_cred(), CAP_SYS_ADMIN,
  882. SECURITY_CAP_NOAUDIT) == 0)
  883. cap_sys_admin = 1;
  884. return __vm_enough_memory(mm, pages, cap_sys_admin);
  885. }
  886. /*
  887. * cap_file_mmap - check if able to map given addr
  888. * @file: unused
  889. * @reqprot: unused
  890. * @prot: unused
  891. * @flags: unused
  892. * @addr: address attempting to be mapped
  893. * @addr_only: unused
  894. *
  895. * If the process is attempting to map memory below mmap_min_addr they need
  896. * CAP_SYS_RAWIO. The other parameters to this function are unused by the
  897. * capability security module. Returns 0 if this mapping should be allowed
  898. * -EPERM if not.
  899. */
  900. int cap_file_mmap(struct file *file, unsigned long reqprot,
  901. unsigned long prot, unsigned long flags,
  902. unsigned long addr, unsigned long addr_only)
  903. {
  904. int ret = 0;
  905. if (addr < dac_mmap_min_addr) {
  906. ret = cap_capable(current, current_cred(), CAP_SYS_RAWIO,
  907. SECURITY_CAP_AUDIT);
  908. /* set PF_SUPERPRIV if it turns out we allow the low mmap */
  909. if (ret == 0)
  910. current->flags |= PF_SUPERPRIV;
  911. }
  912. return ret;
  913. }