commoncap.c 26 KB

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