cred.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. /* Task credentials management - see Documentation/credentials.txt
  2. *
  3. * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/cred.h>
  13. #include <linux/slab.h>
  14. #include <linux/sched.h>
  15. #include <linux/key.h>
  16. #include <linux/keyctl.h>
  17. #include <linux/init_task.h>
  18. #include <linux/security.h>
  19. #include <linux/cn_proc.h>
  20. #if 0
  21. #define kdebug(FMT, ...) \
  22. printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  23. #else
  24. #define kdebug(FMT, ...) \
  25. no_printk("[%-5.5s%5u] "FMT"\n", current->comm, current->pid ,##__VA_ARGS__)
  26. #endif
  27. static struct kmem_cache *cred_jar;
  28. /*
  29. * The common credentials for the initial task's thread group
  30. */
  31. #ifdef CONFIG_KEYS
  32. static struct thread_group_cred init_tgcred = {
  33. .usage = ATOMIC_INIT(2),
  34. .tgid = 0,
  35. .lock = __SPIN_LOCK_UNLOCKED(init_cred.tgcred.lock),
  36. };
  37. #endif
  38. /*
  39. * The initial credentials for the initial task
  40. */
  41. struct cred init_cred = {
  42. .usage = ATOMIC_INIT(4),
  43. #ifdef CONFIG_DEBUG_CREDENTIALS
  44. .subscribers = ATOMIC_INIT(2),
  45. .magic = CRED_MAGIC,
  46. #endif
  47. .securebits = SECUREBITS_DEFAULT,
  48. .cap_inheritable = CAP_INIT_INH_SET,
  49. .cap_permitted = CAP_FULL_SET,
  50. .cap_effective = CAP_INIT_EFF_SET,
  51. .cap_bset = CAP_INIT_BSET,
  52. .user = INIT_USER,
  53. .group_info = &init_groups,
  54. #ifdef CONFIG_KEYS
  55. .tgcred = &init_tgcred,
  56. #endif
  57. };
  58. static inline void set_cred_subscribers(struct cred *cred, int n)
  59. {
  60. #ifdef CONFIG_DEBUG_CREDENTIALS
  61. atomic_set(&cred->subscribers, n);
  62. #endif
  63. }
  64. static inline int read_cred_subscribers(const struct cred *cred)
  65. {
  66. #ifdef CONFIG_DEBUG_CREDENTIALS
  67. return atomic_read(&cred->subscribers);
  68. #else
  69. return 0;
  70. #endif
  71. }
  72. static inline void alter_cred_subscribers(const struct cred *_cred, int n)
  73. {
  74. #ifdef CONFIG_DEBUG_CREDENTIALS
  75. struct cred *cred = (struct cred *) _cred;
  76. atomic_add(n, &cred->subscribers);
  77. #endif
  78. }
  79. /*
  80. * Dispose of the shared task group credentials
  81. */
  82. #ifdef CONFIG_KEYS
  83. static void release_tgcred_rcu(struct rcu_head *rcu)
  84. {
  85. struct thread_group_cred *tgcred =
  86. container_of(rcu, struct thread_group_cred, rcu);
  87. BUG_ON(atomic_read(&tgcred->usage) != 0);
  88. key_put(tgcred->session_keyring);
  89. key_put(tgcred->process_keyring);
  90. kfree(tgcred);
  91. }
  92. #endif
  93. /*
  94. * Release a set of thread group credentials.
  95. */
  96. static void release_tgcred(struct cred *cred)
  97. {
  98. #ifdef CONFIG_KEYS
  99. struct thread_group_cred *tgcred = cred->tgcred;
  100. if (atomic_dec_and_test(&tgcred->usage))
  101. call_rcu(&tgcred->rcu, release_tgcred_rcu);
  102. #endif
  103. }
  104. /*
  105. * The RCU callback to actually dispose of a set of credentials
  106. */
  107. static void put_cred_rcu(struct rcu_head *rcu)
  108. {
  109. struct cred *cred = container_of(rcu, struct cred, rcu);
  110. kdebug("put_cred_rcu(%p)", cred);
  111. #ifdef CONFIG_DEBUG_CREDENTIALS
  112. if (cred->magic != CRED_MAGIC_DEAD ||
  113. atomic_read(&cred->usage) != 0 ||
  114. read_cred_subscribers(cred) != 0)
  115. panic("CRED: put_cred_rcu() sees %p with"
  116. " mag %x, put %p, usage %d, subscr %d\n",
  117. cred, cred->magic, cred->put_addr,
  118. atomic_read(&cred->usage),
  119. read_cred_subscribers(cred));
  120. #else
  121. if (atomic_read(&cred->usage) != 0)
  122. panic("CRED: put_cred_rcu() sees %p with usage %d\n",
  123. cred, atomic_read(&cred->usage));
  124. #endif
  125. security_cred_free(cred);
  126. key_put(cred->thread_keyring);
  127. key_put(cred->request_key_auth);
  128. release_tgcred(cred);
  129. if (cred->group_info)
  130. put_group_info(cred->group_info);
  131. free_uid(cred->user);
  132. kmem_cache_free(cred_jar, cred);
  133. }
  134. /**
  135. * __put_cred - Destroy a set of credentials
  136. * @cred: The record to release
  137. *
  138. * Destroy a set of credentials on which no references remain.
  139. */
  140. void __put_cred(struct cred *cred)
  141. {
  142. kdebug("__put_cred(%p{%d,%d})", cred,
  143. atomic_read(&cred->usage),
  144. read_cred_subscribers(cred));
  145. BUG_ON(atomic_read(&cred->usage) != 0);
  146. #ifdef CONFIG_DEBUG_CREDENTIALS
  147. BUG_ON(read_cred_subscribers(cred) != 0);
  148. cred->magic = CRED_MAGIC_DEAD;
  149. cred->put_addr = __builtin_return_address(0);
  150. #endif
  151. BUG_ON(cred == current->cred);
  152. BUG_ON(cred == current->real_cred);
  153. call_rcu(&cred->rcu, put_cred_rcu);
  154. }
  155. EXPORT_SYMBOL(__put_cred);
  156. /*
  157. * Clean up a task's credentials when it exits
  158. */
  159. void exit_creds(struct task_struct *tsk)
  160. {
  161. struct cred *cred;
  162. kdebug("exit_creds(%u,%p,%p,{%d,%d})", tsk->pid, tsk->real_cred, tsk->cred,
  163. atomic_read(&tsk->cred->usage),
  164. read_cred_subscribers(tsk->cred));
  165. cred = (struct cred *) tsk->real_cred;
  166. tsk->real_cred = NULL;
  167. validate_creds(cred);
  168. alter_cred_subscribers(cred, -1);
  169. put_cred(cred);
  170. cred = (struct cred *) tsk->cred;
  171. tsk->cred = NULL;
  172. validate_creds(cred);
  173. alter_cred_subscribers(cred, -1);
  174. put_cred(cred);
  175. cred = (struct cred *) tsk->replacement_session_keyring;
  176. if (cred) {
  177. tsk->replacement_session_keyring = NULL;
  178. validate_creds(cred);
  179. put_cred(cred);
  180. }
  181. }
  182. /**
  183. * get_task_cred - Get another task's objective credentials
  184. * @task: The task to query
  185. *
  186. * Get the objective credentials of a task, pinning them so that they can't go
  187. * away. Accessing a task's credentials directly is not permitted.
  188. *
  189. * The caller must also make sure task doesn't get deleted, either by holding a
  190. * ref on task or by holding tasklist_lock to prevent it from being unlinked.
  191. */
  192. const struct cred *get_task_cred(struct task_struct *task)
  193. {
  194. const struct cred *cred;
  195. rcu_read_lock();
  196. do {
  197. cred = __task_cred((task));
  198. BUG_ON(!cred);
  199. } while (!atomic_inc_not_zero(&((struct cred *)cred)->usage));
  200. rcu_read_unlock();
  201. return cred;
  202. }
  203. /*
  204. * Allocate blank credentials, such that the credentials can be filled in at a
  205. * later date without risk of ENOMEM.
  206. */
  207. struct cred *cred_alloc_blank(void)
  208. {
  209. struct cred *new;
  210. new = kmem_cache_zalloc(cred_jar, GFP_KERNEL);
  211. if (!new)
  212. return NULL;
  213. #ifdef CONFIG_KEYS
  214. new->tgcred = kzalloc(sizeof(*new->tgcred), GFP_KERNEL);
  215. if (!new->tgcred) {
  216. kmem_cache_free(cred_jar, new);
  217. return NULL;
  218. }
  219. atomic_set(&new->tgcred->usage, 1);
  220. #endif
  221. atomic_set(&new->usage, 1);
  222. #ifdef CONFIG_DEBUG_CREDENTIALS
  223. new->magic = CRED_MAGIC;
  224. #endif
  225. if (security_cred_alloc_blank(new, GFP_KERNEL) < 0)
  226. goto error;
  227. return new;
  228. error:
  229. abort_creds(new);
  230. return NULL;
  231. }
  232. /**
  233. * prepare_creds - Prepare a new set of credentials for modification
  234. *
  235. * Prepare a new set of task credentials for modification. A task's creds
  236. * shouldn't generally be modified directly, therefore this function is used to
  237. * prepare a new copy, which the caller then modifies and then commits by
  238. * calling commit_creds().
  239. *
  240. * Preparation involves making a copy of the objective creds for modification.
  241. *
  242. * Returns a pointer to the new creds-to-be if successful, NULL otherwise.
  243. *
  244. * Call commit_creds() or abort_creds() to clean up.
  245. */
  246. struct cred *prepare_creds(void)
  247. {
  248. struct task_struct *task = current;
  249. const struct cred *old;
  250. struct cred *new;
  251. validate_process_creds();
  252. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  253. if (!new)
  254. return NULL;
  255. kdebug("prepare_creds() alloc %p", new);
  256. old = task->cred;
  257. memcpy(new, old, sizeof(struct cred));
  258. atomic_set(&new->usage, 1);
  259. set_cred_subscribers(new, 0);
  260. get_group_info(new->group_info);
  261. get_uid(new->user);
  262. #ifdef CONFIG_KEYS
  263. key_get(new->thread_keyring);
  264. key_get(new->request_key_auth);
  265. atomic_inc(&new->tgcred->usage);
  266. #endif
  267. #ifdef CONFIG_SECURITY
  268. new->security = NULL;
  269. #endif
  270. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  271. goto error;
  272. validate_creds(new);
  273. return new;
  274. error:
  275. abort_creds(new);
  276. return NULL;
  277. }
  278. EXPORT_SYMBOL(prepare_creds);
  279. /*
  280. * Prepare credentials for current to perform an execve()
  281. * - The caller must hold ->cred_guard_mutex
  282. */
  283. struct cred *prepare_exec_creds(void)
  284. {
  285. struct thread_group_cred *tgcred = NULL;
  286. struct cred *new;
  287. #ifdef CONFIG_KEYS
  288. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  289. if (!tgcred)
  290. return NULL;
  291. #endif
  292. new = prepare_creds();
  293. if (!new) {
  294. kfree(tgcred);
  295. return new;
  296. }
  297. #ifdef CONFIG_KEYS
  298. /* newly exec'd tasks don't get a thread keyring */
  299. key_put(new->thread_keyring);
  300. new->thread_keyring = NULL;
  301. /* create a new per-thread-group creds for all this set of threads to
  302. * share */
  303. memcpy(tgcred, new->tgcred, sizeof(struct thread_group_cred));
  304. atomic_set(&tgcred->usage, 1);
  305. spin_lock_init(&tgcred->lock);
  306. /* inherit the session keyring; new process keyring */
  307. key_get(tgcred->session_keyring);
  308. tgcred->process_keyring = NULL;
  309. release_tgcred(new);
  310. new->tgcred = tgcred;
  311. #endif
  312. return new;
  313. }
  314. /*
  315. * Copy credentials for the new process created by fork()
  316. *
  317. * We share if we can, but under some circumstances we have to generate a new
  318. * set.
  319. *
  320. * The new process gets the current process's subjective credentials as its
  321. * objective and subjective credentials
  322. */
  323. int copy_creds(struct task_struct *p, unsigned long clone_flags)
  324. {
  325. #ifdef CONFIG_KEYS
  326. struct thread_group_cred *tgcred;
  327. #endif
  328. struct cred *new;
  329. int ret;
  330. if (
  331. #ifdef CONFIG_KEYS
  332. !p->cred->thread_keyring &&
  333. #endif
  334. clone_flags & CLONE_THREAD
  335. ) {
  336. p->real_cred = get_cred(p->cred);
  337. get_cred(p->cred);
  338. alter_cred_subscribers(p->cred, 2);
  339. kdebug("share_creds(%p{%d,%d})",
  340. p->cred, atomic_read(&p->cred->usage),
  341. read_cred_subscribers(p->cred));
  342. atomic_inc(&p->cred->user->processes);
  343. return 0;
  344. }
  345. new = prepare_creds();
  346. if (!new)
  347. return -ENOMEM;
  348. if (clone_flags & CLONE_NEWUSER) {
  349. ret = create_user_ns(new);
  350. if (ret < 0)
  351. goto error_put;
  352. }
  353. #ifdef CONFIG_KEYS
  354. /* new threads get their own thread keyrings if their parent already
  355. * had one */
  356. if (new->thread_keyring) {
  357. key_put(new->thread_keyring);
  358. new->thread_keyring = NULL;
  359. if (clone_flags & CLONE_THREAD)
  360. install_thread_keyring_to_cred(new);
  361. }
  362. /* we share the process and session keyrings between all the threads in
  363. * a process - this is slightly icky as we violate COW credentials a
  364. * bit */
  365. if (!(clone_flags & CLONE_THREAD)) {
  366. tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
  367. if (!tgcred) {
  368. ret = -ENOMEM;
  369. goto error_put;
  370. }
  371. atomic_set(&tgcred->usage, 1);
  372. spin_lock_init(&tgcred->lock);
  373. tgcred->process_keyring = NULL;
  374. tgcred->session_keyring = key_get(new->tgcred->session_keyring);
  375. release_tgcred(new);
  376. new->tgcred = tgcred;
  377. }
  378. #endif
  379. atomic_inc(&new->user->processes);
  380. p->cred = p->real_cred = get_cred(new);
  381. alter_cred_subscribers(new, 2);
  382. validate_creds(new);
  383. return 0;
  384. error_put:
  385. put_cred(new);
  386. return ret;
  387. }
  388. /**
  389. * commit_creds - Install new credentials upon the current task
  390. * @new: The credentials to be assigned
  391. *
  392. * Install a new set of credentials to the current task, using RCU to replace
  393. * the old set. Both the objective and the subjective credentials pointers are
  394. * updated. This function may not be called if the subjective credentials are
  395. * in an overridden state.
  396. *
  397. * This function eats the caller's reference to the new credentials.
  398. *
  399. * Always returns 0 thus allowing this function to be tail-called at the end
  400. * of, say, sys_setgid().
  401. */
  402. int commit_creds(struct cred *new)
  403. {
  404. struct task_struct *task = current;
  405. const struct cred *old = task->real_cred;
  406. kdebug("commit_creds(%p{%d,%d})", new,
  407. atomic_read(&new->usage),
  408. read_cred_subscribers(new));
  409. BUG_ON(task->cred != old);
  410. #ifdef CONFIG_DEBUG_CREDENTIALS
  411. BUG_ON(read_cred_subscribers(old) < 2);
  412. validate_creds(old);
  413. validate_creds(new);
  414. #endif
  415. BUG_ON(atomic_read(&new->usage) < 1);
  416. get_cred(new); /* we will require a ref for the subj creds too */
  417. /* dumpability changes */
  418. if (old->euid != new->euid ||
  419. old->egid != new->egid ||
  420. old->fsuid != new->fsuid ||
  421. old->fsgid != new->fsgid ||
  422. !cap_issubset(new->cap_permitted, old->cap_permitted)) {
  423. if (task->mm)
  424. set_dumpable(task->mm, suid_dumpable);
  425. task->pdeath_signal = 0;
  426. smp_wmb();
  427. }
  428. /* alter the thread keyring */
  429. if (new->fsuid != old->fsuid)
  430. key_fsuid_changed(task);
  431. if (new->fsgid != old->fsgid)
  432. key_fsgid_changed(task);
  433. /* do it
  434. * - What if a process setreuid()'s and this brings the
  435. * new uid over his NPROC rlimit? We can check this now
  436. * cheaply with the new uid cache, so if it matters
  437. * we should be checking for it. -DaveM
  438. */
  439. alter_cred_subscribers(new, 2);
  440. if (new->user != old->user)
  441. atomic_inc(&new->user->processes);
  442. rcu_assign_pointer(task->real_cred, new);
  443. rcu_assign_pointer(task->cred, new);
  444. if (new->user != old->user)
  445. atomic_dec(&old->user->processes);
  446. alter_cred_subscribers(old, -2);
  447. /* send notifications */
  448. if (new->uid != old->uid ||
  449. new->euid != old->euid ||
  450. new->suid != old->suid ||
  451. new->fsuid != old->fsuid)
  452. proc_id_connector(task, PROC_EVENT_UID);
  453. if (new->gid != old->gid ||
  454. new->egid != old->egid ||
  455. new->sgid != old->sgid ||
  456. new->fsgid != old->fsgid)
  457. proc_id_connector(task, PROC_EVENT_GID);
  458. /* release the old obj and subj refs both */
  459. put_cred(old);
  460. put_cred(old);
  461. return 0;
  462. }
  463. EXPORT_SYMBOL(commit_creds);
  464. /**
  465. * abort_creds - Discard a set of credentials and unlock the current task
  466. * @new: The credentials that were going to be applied
  467. *
  468. * Discard a set of credentials that were under construction and unlock the
  469. * current task.
  470. */
  471. void abort_creds(struct cred *new)
  472. {
  473. kdebug("abort_creds(%p{%d,%d})", new,
  474. atomic_read(&new->usage),
  475. read_cred_subscribers(new));
  476. #ifdef CONFIG_DEBUG_CREDENTIALS
  477. BUG_ON(read_cred_subscribers(new) != 0);
  478. #endif
  479. BUG_ON(atomic_read(&new->usage) < 1);
  480. put_cred(new);
  481. }
  482. EXPORT_SYMBOL(abort_creds);
  483. /**
  484. * override_creds - Override the current process's subjective credentials
  485. * @new: The credentials to be assigned
  486. *
  487. * Install a set of temporary override subjective credentials on the current
  488. * process, returning the old set for later reversion.
  489. */
  490. const struct cred *override_creds(const struct cred *new)
  491. {
  492. const struct cred *old = current->cred;
  493. kdebug("override_creds(%p{%d,%d})", new,
  494. atomic_read(&new->usage),
  495. read_cred_subscribers(new));
  496. validate_creds(old);
  497. validate_creds(new);
  498. get_cred(new);
  499. alter_cred_subscribers(new, 1);
  500. rcu_assign_pointer(current->cred, new);
  501. alter_cred_subscribers(old, -1);
  502. kdebug("override_creds() = %p{%d,%d}", old,
  503. atomic_read(&old->usage),
  504. read_cred_subscribers(old));
  505. return old;
  506. }
  507. EXPORT_SYMBOL(override_creds);
  508. /**
  509. * revert_creds - Revert a temporary subjective credentials override
  510. * @old: The credentials to be restored
  511. *
  512. * Revert a temporary set of override subjective credentials to an old set,
  513. * discarding the override set.
  514. */
  515. void revert_creds(const struct cred *old)
  516. {
  517. const struct cred *override = current->cred;
  518. kdebug("revert_creds(%p{%d,%d})", old,
  519. atomic_read(&old->usage),
  520. read_cred_subscribers(old));
  521. validate_creds(old);
  522. validate_creds(override);
  523. alter_cred_subscribers(old, 1);
  524. rcu_assign_pointer(current->cred, old);
  525. alter_cred_subscribers(override, -1);
  526. put_cred(override);
  527. }
  528. EXPORT_SYMBOL(revert_creds);
  529. /*
  530. * initialise the credentials stuff
  531. */
  532. void __init cred_init(void)
  533. {
  534. /* allocate a slab in which we can store credentials */
  535. cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
  536. 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
  537. }
  538. /**
  539. * prepare_kernel_cred - Prepare a set of credentials for a kernel service
  540. * @daemon: A userspace daemon to be used as a reference
  541. *
  542. * Prepare a set of credentials for a kernel service. This can then be used to
  543. * override a task's own credentials so that work can be done on behalf of that
  544. * task that requires a different subjective context.
  545. *
  546. * @daemon is used to provide a base for the security record, but can be NULL.
  547. * If @daemon is supplied, then the security data will be derived from that;
  548. * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
  549. *
  550. * The caller may change these controls afterwards if desired.
  551. *
  552. * Returns the new credentials or NULL if out of memory.
  553. *
  554. * Does not take, and does not return holding current->cred_replace_mutex.
  555. */
  556. struct cred *prepare_kernel_cred(struct task_struct *daemon)
  557. {
  558. const struct cred *old;
  559. struct cred *new;
  560. new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
  561. if (!new)
  562. return NULL;
  563. kdebug("prepare_kernel_cred() alloc %p", new);
  564. if (daemon)
  565. old = get_task_cred(daemon);
  566. else
  567. old = get_cred(&init_cred);
  568. validate_creds(old);
  569. *new = *old;
  570. atomic_set(&new->usage, 1);
  571. set_cred_subscribers(new, 0);
  572. get_uid(new->user);
  573. get_group_info(new->group_info);
  574. #ifdef CONFIG_KEYS
  575. atomic_inc(&init_tgcred.usage);
  576. new->tgcred = &init_tgcred;
  577. new->request_key_auth = NULL;
  578. new->thread_keyring = NULL;
  579. new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
  580. #endif
  581. #ifdef CONFIG_SECURITY
  582. new->security = NULL;
  583. #endif
  584. if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
  585. goto error;
  586. put_cred(old);
  587. validate_creds(new);
  588. return new;
  589. error:
  590. put_cred(new);
  591. put_cred(old);
  592. return NULL;
  593. }
  594. EXPORT_SYMBOL(prepare_kernel_cred);
  595. /**
  596. * set_security_override - Set the security ID in a set of credentials
  597. * @new: The credentials to alter
  598. * @secid: The LSM security ID to set
  599. *
  600. * Set the LSM security ID in a set of credentials so that the subjective
  601. * security is overridden when an alternative set of credentials is used.
  602. */
  603. int set_security_override(struct cred *new, u32 secid)
  604. {
  605. return security_kernel_act_as(new, secid);
  606. }
  607. EXPORT_SYMBOL(set_security_override);
  608. /**
  609. * set_security_override_from_ctx - Set the security ID in a set of credentials
  610. * @new: The credentials to alter
  611. * @secctx: The LSM security context to generate the security ID from.
  612. *
  613. * Set the LSM security ID in a set of credentials so that the subjective
  614. * security is overridden when an alternative set of credentials is used. The
  615. * security ID is specified in string form as a security context to be
  616. * interpreted by the LSM.
  617. */
  618. int set_security_override_from_ctx(struct cred *new, const char *secctx)
  619. {
  620. u32 secid;
  621. int ret;
  622. ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
  623. if (ret < 0)
  624. return ret;
  625. return set_security_override(new, secid);
  626. }
  627. EXPORT_SYMBOL(set_security_override_from_ctx);
  628. /**
  629. * set_create_files_as - Set the LSM file create context in a set of credentials
  630. * @new: The credentials to alter
  631. * @inode: The inode to take the context from
  632. *
  633. * Change the LSM file creation context in a set of credentials to be the same
  634. * as the object context of the specified inode, so that the new inodes have
  635. * the same MAC context as that inode.
  636. */
  637. int set_create_files_as(struct cred *new, struct inode *inode)
  638. {
  639. new->fsuid = inode->i_uid;
  640. new->fsgid = inode->i_gid;
  641. return security_kernel_create_files_as(new, inode);
  642. }
  643. EXPORT_SYMBOL(set_create_files_as);
  644. #ifdef CONFIG_DEBUG_CREDENTIALS
  645. bool creds_are_invalid(const struct cred *cred)
  646. {
  647. if (cred->magic != CRED_MAGIC)
  648. return true;
  649. #ifdef CONFIG_SECURITY_SELINUX
  650. /*
  651. * cred->security == NULL if security_cred_alloc_blank() or
  652. * security_prepare_creds() returned an error.
  653. */
  654. if (selinux_is_enabled() && cred->security) {
  655. if ((unsigned long) cred->security < PAGE_SIZE)
  656. return true;
  657. if ((*(u32 *)cred->security & 0xffffff00) ==
  658. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8))
  659. return true;
  660. }
  661. #endif
  662. return false;
  663. }
  664. EXPORT_SYMBOL(creds_are_invalid);
  665. /*
  666. * dump invalid credentials
  667. */
  668. static void dump_invalid_creds(const struct cred *cred, const char *label,
  669. const struct task_struct *tsk)
  670. {
  671. printk(KERN_ERR "CRED: %s credentials: %p %s%s%s\n",
  672. label, cred,
  673. cred == &init_cred ? "[init]" : "",
  674. cred == tsk->real_cred ? "[real]" : "",
  675. cred == tsk->cred ? "[eff]" : "");
  676. printk(KERN_ERR "CRED: ->magic=%x, put_addr=%p\n",
  677. cred->magic, cred->put_addr);
  678. printk(KERN_ERR "CRED: ->usage=%d, subscr=%d\n",
  679. atomic_read(&cred->usage),
  680. read_cred_subscribers(cred));
  681. printk(KERN_ERR "CRED: ->*uid = { %d,%d,%d,%d }\n",
  682. cred->uid, cred->euid, cred->suid, cred->fsuid);
  683. printk(KERN_ERR "CRED: ->*gid = { %d,%d,%d,%d }\n",
  684. cred->gid, cred->egid, cred->sgid, cred->fsgid);
  685. #ifdef CONFIG_SECURITY
  686. printk(KERN_ERR "CRED: ->security is %p\n", cred->security);
  687. if ((unsigned long) cred->security >= PAGE_SIZE &&
  688. (((unsigned long) cred->security & 0xffffff00) !=
  689. (POISON_FREE << 24 | POISON_FREE << 16 | POISON_FREE << 8)))
  690. printk(KERN_ERR "CRED: ->security {%x, %x}\n",
  691. ((u32*)cred->security)[0],
  692. ((u32*)cred->security)[1]);
  693. #endif
  694. }
  695. /*
  696. * report use of invalid credentials
  697. */
  698. void __invalid_creds(const struct cred *cred, const char *file, unsigned line)
  699. {
  700. printk(KERN_ERR "CRED: Invalid credentials\n");
  701. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  702. dump_invalid_creds(cred, "Specified", current);
  703. BUG();
  704. }
  705. EXPORT_SYMBOL(__invalid_creds);
  706. /*
  707. * check the credentials on a process
  708. */
  709. void __validate_process_creds(struct task_struct *tsk,
  710. const char *file, unsigned line)
  711. {
  712. if (tsk->cred == tsk->real_cred) {
  713. if (unlikely(read_cred_subscribers(tsk->cred) < 2 ||
  714. creds_are_invalid(tsk->cred)))
  715. goto invalid_creds;
  716. } else {
  717. if (unlikely(read_cred_subscribers(tsk->real_cred) < 1 ||
  718. read_cred_subscribers(tsk->cred) < 1 ||
  719. creds_are_invalid(tsk->real_cred) ||
  720. creds_are_invalid(tsk->cred)))
  721. goto invalid_creds;
  722. }
  723. return;
  724. invalid_creds:
  725. printk(KERN_ERR "CRED: Invalid process credentials\n");
  726. printk(KERN_ERR "CRED: At %s:%u\n", file, line);
  727. dump_invalid_creds(tsk->real_cred, "Real", tsk);
  728. if (tsk->cred != tsk->real_cred)
  729. dump_invalid_creds(tsk->cred, "Effective", tsk);
  730. else
  731. printk(KERN_ERR "CRED: Effective creds == Real creds\n");
  732. BUG();
  733. }
  734. EXPORT_SYMBOL(__validate_process_creds);
  735. /*
  736. * check creds for do_exit()
  737. */
  738. void validate_creds_for_do_exit(struct task_struct *tsk)
  739. {
  740. kdebug("validate_creds_for_do_exit(%p,%p{%d,%d})",
  741. tsk->real_cred, tsk->cred,
  742. atomic_read(&tsk->cred->usage),
  743. read_cred_subscribers(tsk->cred));
  744. __validate_process_creds(tsk, __FILE__, __LINE__);
  745. }
  746. #endif /* CONFIG_DEBUG_CREDENTIALS */