messaging.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /**
  2. * eCryptfs: Linux filesystem encryption layer
  3. *
  4. * Copyright (C) 2004-2008 International Business Machines Corp.
  5. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  6. * Tyler Hicks <tyhicks@ou.edu>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20. * 02111-1307, USA.
  21. */
  22. #include <linux/sched.h>
  23. #include <linux/user_namespace.h>
  24. #include <linux/nsproxy.h>
  25. #include "ecryptfs_kernel.h"
  26. static LIST_HEAD(ecryptfs_msg_ctx_free_list);
  27. static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
  28. static struct mutex ecryptfs_msg_ctx_lists_mux;
  29. static struct hlist_head *ecryptfs_daemon_hash;
  30. struct mutex ecryptfs_daemon_hash_mux;
  31. static int ecryptfs_hash_buckets;
  32. #define ecryptfs_uid_hash(uid) \
  33. hash_long((unsigned long)uid, ecryptfs_hash_buckets)
  34. static u32 ecryptfs_msg_counter;
  35. static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
  36. /**
  37. * ecryptfs_acquire_free_msg_ctx
  38. * @msg_ctx: The context that was acquired from the free list
  39. *
  40. * Acquires a context element from the free list and locks the mutex
  41. * on the context. Sets the msg_ctx task to current. Returns zero on
  42. * success; non-zero on error or upon failure to acquire a free
  43. * context element. Must be called with ecryptfs_msg_ctx_lists_mux
  44. * held.
  45. */
  46. static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
  47. {
  48. struct list_head *p;
  49. int rc;
  50. if (list_empty(&ecryptfs_msg_ctx_free_list)) {
  51. printk(KERN_WARNING "%s: The eCryptfs free "
  52. "context list is empty. It may be helpful to "
  53. "specify the ecryptfs_message_buf_len "
  54. "parameter to be greater than the current "
  55. "value of [%d]\n", __func__, ecryptfs_message_buf_len);
  56. rc = -ENOMEM;
  57. goto out;
  58. }
  59. list_for_each(p, &ecryptfs_msg_ctx_free_list) {
  60. *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
  61. if (mutex_trylock(&(*msg_ctx)->mux)) {
  62. (*msg_ctx)->task = current;
  63. rc = 0;
  64. goto out;
  65. }
  66. }
  67. rc = -ENOMEM;
  68. out:
  69. return rc;
  70. }
  71. /**
  72. * ecryptfs_msg_ctx_free_to_alloc
  73. * @msg_ctx: The context to move from the free list to the alloc list
  74. *
  75. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  76. */
  77. static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
  78. {
  79. list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
  80. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
  81. msg_ctx->counter = ++ecryptfs_msg_counter;
  82. }
  83. /**
  84. * ecryptfs_msg_ctx_alloc_to_free
  85. * @msg_ctx: The context to move from the alloc list to the free list
  86. *
  87. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  88. */
  89. void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
  90. {
  91. list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
  92. if (msg_ctx->msg)
  93. kfree(msg_ctx->msg);
  94. msg_ctx->msg = NULL;
  95. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
  96. }
  97. /**
  98. * ecryptfs_find_daemon_by_euid
  99. * @euid: The effective user id which maps to the desired daemon id
  100. * @user_ns: The namespace in which @euid applies
  101. * @daemon: If return value is zero, points to the desired daemon pointer
  102. *
  103. * Must be called with ecryptfs_daemon_hash_mux held.
  104. *
  105. * Search the hash list for the given user id.
  106. *
  107. * Returns zero if the user id exists in the list; non-zero otherwise.
  108. */
  109. int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
  110. struct user_namespace *user_ns)
  111. {
  112. struct hlist_node *elem;
  113. int rc;
  114. hlist_for_each_entry(*daemon, elem,
  115. &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
  116. euid_chain) {
  117. if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
  118. rc = 0;
  119. goto out;
  120. }
  121. }
  122. rc = -EINVAL;
  123. out:
  124. return rc;
  125. }
  126. static int
  127. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  128. struct ecryptfs_msg_ctx **msg_ctx);
  129. /**
  130. * ecryptfs_send_raw_message
  131. * @msg_type: Message type
  132. * @daemon: Daemon struct for recipient of message
  133. *
  134. * A raw message is one that does not include an ecryptfs_message
  135. * struct. It simply has a type.
  136. *
  137. * Must be called with ecryptfs_daemon_hash_mux held.
  138. *
  139. * Returns zero on success; non-zero otherwise
  140. */
  141. static int ecryptfs_send_raw_message(u8 msg_type,
  142. struct ecryptfs_daemon *daemon)
  143. {
  144. struct ecryptfs_msg_ctx *msg_ctx;
  145. int rc;
  146. rc = ecryptfs_send_message_locked(NULL, 0, msg_type, &msg_ctx);
  147. if (rc) {
  148. printk(KERN_ERR "%s: Error whilst attempting to send "
  149. "message to ecryptfsd; rc = [%d]\n", __func__, rc);
  150. goto out;
  151. }
  152. /* Raw messages are logically context-free (e.g., no
  153. * reply is expected), so we set the state of the
  154. * ecryptfs_msg_ctx object to indicate that it should
  155. * be freed as soon as the message is sent. */
  156. mutex_lock(&msg_ctx->mux);
  157. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_NO_REPLY;
  158. mutex_unlock(&msg_ctx->mux);
  159. out:
  160. return rc;
  161. }
  162. /**
  163. * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
  164. * @daemon: Pointer to set to newly allocated daemon struct
  165. * @euid: Effective user id for the daemon
  166. * @user_ns: The namespace in which @euid applies
  167. * @pid: Process id for the daemon
  168. *
  169. * Must be called ceremoniously while in possession of
  170. * ecryptfs_sacred_daemon_hash_mux
  171. *
  172. * Returns zero on success; non-zero otherwise
  173. */
  174. int
  175. ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
  176. struct user_namespace *user_ns, struct pid *pid)
  177. {
  178. int rc = 0;
  179. (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
  180. if (!(*daemon)) {
  181. rc = -ENOMEM;
  182. printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
  183. "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
  184. goto out;
  185. }
  186. (*daemon)->euid = euid;
  187. (*daemon)->user_ns = get_user_ns(user_ns);
  188. (*daemon)->pid = get_pid(pid);
  189. (*daemon)->task = current;
  190. mutex_init(&(*daemon)->mux);
  191. INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
  192. init_waitqueue_head(&(*daemon)->wait);
  193. (*daemon)->num_queued_msg_ctx = 0;
  194. hlist_add_head(&(*daemon)->euid_chain,
  195. &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
  196. out:
  197. return rc;
  198. }
  199. /**
  200. * ecryptfs_process_helo
  201. * @euid: The user ID owner of the message
  202. * @user_ns: The namespace in which @euid applies
  203. * @pid: The process ID for the userspace program that sent the
  204. * message
  205. *
  206. * Adds the euid and pid values to the daemon euid hash. If an euid
  207. * already has a daemon pid registered, the daemon will be
  208. * unregistered before the new daemon is put into the hash list.
  209. * Returns zero after adding a new daemon to the hash list;
  210. * non-zero otherwise.
  211. */
  212. int ecryptfs_process_helo(uid_t euid, struct user_namespace *user_ns,
  213. struct pid *pid)
  214. {
  215. struct ecryptfs_daemon *new_daemon;
  216. struct ecryptfs_daemon *old_daemon;
  217. int rc;
  218. mutex_lock(&ecryptfs_daemon_hash_mux);
  219. rc = ecryptfs_find_daemon_by_euid(&old_daemon, euid, user_ns);
  220. if (rc != 0) {
  221. printk(KERN_WARNING "Received request from user [%d] "
  222. "to register daemon [0x%p]; unregistering daemon "
  223. "[0x%p]\n", euid, pid, old_daemon->pid);
  224. rc = ecryptfs_send_raw_message(ECRYPTFS_MSG_QUIT, old_daemon);
  225. if (rc)
  226. printk(KERN_WARNING "Failed to send QUIT "
  227. "message to daemon [0x%p]; rc = [%d]\n",
  228. old_daemon->pid, rc);
  229. hlist_del(&old_daemon->euid_chain);
  230. kfree(old_daemon);
  231. }
  232. rc = ecryptfs_spawn_daemon(&new_daemon, euid, user_ns, pid);
  233. if (rc)
  234. printk(KERN_ERR "%s: The gods are displeased with this attempt "
  235. "to create a new daemon object for euid [%d]; pid "
  236. "[0x%p]; rc = [%d]\n", __func__, euid, pid, rc);
  237. mutex_unlock(&ecryptfs_daemon_hash_mux);
  238. return rc;
  239. }
  240. /**
  241. * ecryptfs_exorcise_daemon - Destroy the daemon struct
  242. *
  243. * Must be called ceremoniously while in possession of
  244. * ecryptfs_daemon_hash_mux and the daemon's own mux.
  245. */
  246. int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
  247. {
  248. struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
  249. int rc = 0;
  250. mutex_lock(&daemon->mux);
  251. if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  252. || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
  253. rc = -EBUSY;
  254. printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
  255. "[0x%p], but it is in the midst of a read or a poll\n",
  256. __func__, daemon->pid);
  257. mutex_unlock(&daemon->mux);
  258. goto out;
  259. }
  260. list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
  261. &daemon->msg_ctx_out_queue, daemon_out_list) {
  262. list_del(&msg_ctx->daemon_out_list);
  263. daemon->num_queued_msg_ctx--;
  264. printk(KERN_WARNING "%s: Warning: dropping message that is in "
  265. "the out queue of a dying daemon\n", __func__);
  266. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  267. }
  268. hlist_del(&daemon->euid_chain);
  269. if (daemon->task)
  270. wake_up_process(daemon->task);
  271. if (daemon->pid)
  272. put_pid(daemon->pid);
  273. if (daemon->user_ns)
  274. put_user_ns(daemon->user_ns);
  275. mutex_unlock(&daemon->mux);
  276. memset(daemon, 0, sizeof(*daemon));
  277. kfree(daemon);
  278. out:
  279. return rc;
  280. }
  281. /**
  282. * ecryptfs_process_quit
  283. * @euid: The user ID owner of the message
  284. * @user_ns: The namespace in which @euid applies
  285. * @pid: The process ID for the userspace program that sent the
  286. * message
  287. *
  288. * Deletes the corresponding daemon for the given euid and pid, if
  289. * it is the registered that is requesting the deletion. Returns zero
  290. * after deleting the desired daemon; non-zero otherwise.
  291. */
  292. int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
  293. struct pid *pid)
  294. {
  295. struct ecryptfs_daemon *daemon;
  296. int rc;
  297. mutex_lock(&ecryptfs_daemon_hash_mux);
  298. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
  299. if (rc || !daemon) {
  300. rc = -EINVAL;
  301. printk(KERN_ERR "Received request from user [%d] to "
  302. "unregister unrecognized daemon [0x%p]\n", euid, pid);
  303. goto out_unlock;
  304. }
  305. rc = ecryptfs_exorcise_daemon(daemon);
  306. out_unlock:
  307. mutex_unlock(&ecryptfs_daemon_hash_mux);
  308. return rc;
  309. }
  310. /**
  311. * ecryptfs_process_reponse
  312. * @msg: The ecryptfs message received; the caller should sanity check
  313. * msg->data_len and free the memory
  314. * @pid: The process ID of the userspace application that sent the
  315. * message
  316. * @seq: The sequence number of the message; must match the sequence
  317. * number for the existing message context waiting for this
  318. * response
  319. *
  320. * Processes a response message after sending an operation request to
  321. * userspace. Some other process is awaiting this response. Before
  322. * sending out its first communications, the other process allocated a
  323. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  324. * response message contains this index so that we can copy over the
  325. * response message into the msg_ctx that the process holds a
  326. * reference to. The other process is going to wake up, check to see
  327. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  328. * proceed to read off and process the response message. Returns zero
  329. * upon delivery to desired context element; non-zero upon delivery
  330. * failure or error.
  331. *
  332. * Returns zero on success; non-zero otherwise
  333. */
  334. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
  335. struct user_namespace *user_ns, struct pid *pid,
  336. u32 seq)
  337. {
  338. struct ecryptfs_daemon *daemon;
  339. struct ecryptfs_msg_ctx *msg_ctx;
  340. size_t msg_size;
  341. struct nsproxy *nsproxy;
  342. struct user_namespace *tsk_user_ns;
  343. uid_t ctx_euid;
  344. int rc;
  345. if (msg->index >= ecryptfs_message_buf_len) {
  346. rc = -EINVAL;
  347. printk(KERN_ERR "%s: Attempt to reference "
  348. "context buffer at index [%d]; maximum "
  349. "allowable is [%d]\n", __func__, msg->index,
  350. (ecryptfs_message_buf_len - 1));
  351. goto out;
  352. }
  353. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  354. mutex_lock(&msg_ctx->mux);
  355. mutex_lock(&ecryptfs_daemon_hash_mux);
  356. rcu_read_lock();
  357. nsproxy = task_nsproxy(msg_ctx->task);
  358. if (nsproxy == NULL) {
  359. rc = -EBADMSG;
  360. printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
  361. "message.\n", __func__);
  362. rcu_read_unlock();
  363. mutex_unlock(&ecryptfs_daemon_hash_mux);
  364. goto wake_up;
  365. }
  366. tsk_user_ns = __task_cred(msg_ctx->task)->user->user_ns;
  367. ctx_euid = task_euid(msg_ctx->task);
  368. rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
  369. rcu_read_unlock();
  370. mutex_unlock(&ecryptfs_daemon_hash_mux);
  371. if (rc) {
  372. rc = -EBADMSG;
  373. printk(KERN_WARNING "%s: User [%d] received a "
  374. "message response from process [0x%p] but does "
  375. "not have a registered daemon\n", __func__,
  376. ctx_euid, pid);
  377. goto wake_up;
  378. }
  379. if (ctx_euid != euid) {
  380. rc = -EBADMSG;
  381. printk(KERN_WARNING "%s: Received message from user "
  382. "[%d]; expected message from user [%d]\n", __func__,
  383. euid, ctx_euid);
  384. goto unlock;
  385. }
  386. if (tsk_user_ns != user_ns) {
  387. rc = -EBADMSG;
  388. printk(KERN_WARNING "%s: Received message from user_ns "
  389. "[0x%p]; expected message from user_ns [0x%p]\n",
  390. __func__, user_ns, tsk_user_ns);
  391. goto unlock;
  392. }
  393. if (daemon->pid != pid) {
  394. rc = -EBADMSG;
  395. printk(KERN_ERR "%s: User [%d] sent a message response "
  396. "from an unrecognized process [0x%p]\n",
  397. __func__, ctx_euid, pid);
  398. goto unlock;
  399. }
  400. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  401. rc = -EINVAL;
  402. printk(KERN_WARNING "%s: Desired context element is not "
  403. "pending a response\n", __func__);
  404. goto unlock;
  405. } else if (msg_ctx->counter != seq) {
  406. rc = -EINVAL;
  407. printk(KERN_WARNING "%s: Invalid message sequence; "
  408. "expected [%d]; received [%d]\n", __func__,
  409. msg_ctx->counter, seq);
  410. goto unlock;
  411. }
  412. msg_size = (sizeof(*msg) + msg->data_len);
  413. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  414. if (!msg_ctx->msg) {
  415. rc = -ENOMEM;
  416. printk(KERN_ERR "%s: Failed to allocate [%Zd] bytes of "
  417. "GFP_KERNEL memory\n", __func__, msg_size);
  418. goto unlock;
  419. }
  420. memcpy(msg_ctx->msg, msg, msg_size);
  421. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  422. rc = 0;
  423. wake_up:
  424. wake_up_process(msg_ctx->task);
  425. unlock:
  426. mutex_unlock(&msg_ctx->mux);
  427. out:
  428. return rc;
  429. }
  430. /**
  431. * ecryptfs_send_message_locked
  432. * @data: The data to send
  433. * @data_len: The length of data
  434. * @msg_ctx: The message context allocated for the send
  435. *
  436. * Must be called with ecryptfs_daemon_hash_mux held.
  437. *
  438. * Returns zero on success; non-zero otherwise
  439. */
  440. static int
  441. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  442. struct ecryptfs_msg_ctx **msg_ctx)
  443. {
  444. struct ecryptfs_daemon *daemon;
  445. uid_t euid = current_euid();
  446. int rc;
  447. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  448. if (rc || !daemon) {
  449. rc = -ENOTCONN;
  450. printk(KERN_ERR "%s: User [%d] does not have a daemon "
  451. "registered\n", __func__, euid);
  452. goto out;
  453. }
  454. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  455. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  456. if (rc) {
  457. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  458. printk(KERN_WARNING "%s: Could not claim a free "
  459. "context element\n", __func__);
  460. goto out;
  461. }
  462. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  463. mutex_unlock(&(*msg_ctx)->mux);
  464. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  465. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  466. daemon);
  467. if (rc)
  468. printk(KERN_ERR "%s: Error attempting to send message to "
  469. "userspace daemon; rc = [%d]\n", __func__, rc);
  470. out:
  471. return rc;
  472. }
  473. /**
  474. * ecryptfs_send_message
  475. * @data: The data to send
  476. * @data_len: The length of data
  477. * @msg_ctx: The message context allocated for the send
  478. *
  479. * Grabs ecryptfs_daemon_hash_mux.
  480. *
  481. * Returns zero on success; non-zero otherwise
  482. */
  483. int ecryptfs_send_message(char *data, int data_len,
  484. struct ecryptfs_msg_ctx **msg_ctx)
  485. {
  486. int rc;
  487. mutex_lock(&ecryptfs_daemon_hash_mux);
  488. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  489. msg_ctx);
  490. mutex_unlock(&ecryptfs_daemon_hash_mux);
  491. return rc;
  492. }
  493. /**
  494. * ecryptfs_wait_for_response
  495. * @msg_ctx: The context that was assigned when sending a message
  496. * @msg: The incoming message from userspace; not set if rc != 0
  497. *
  498. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  499. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  500. * returned, msg will point to a valid message from userspace; a
  501. * non-zero value is returned upon failure to receive a message or an
  502. * error occurs. Callee must free @msg on success.
  503. */
  504. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  505. struct ecryptfs_message **msg)
  506. {
  507. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  508. int rc = 0;
  509. sleep:
  510. timeout = schedule_timeout_interruptible(timeout);
  511. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  512. mutex_lock(&msg_ctx->mux);
  513. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  514. if (timeout) {
  515. mutex_unlock(&msg_ctx->mux);
  516. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  517. goto sleep;
  518. }
  519. rc = -ENOMSG;
  520. } else {
  521. *msg = msg_ctx->msg;
  522. msg_ctx->msg = NULL;
  523. }
  524. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  525. mutex_unlock(&msg_ctx->mux);
  526. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  527. return rc;
  528. }
  529. int ecryptfs_init_messaging(void)
  530. {
  531. int i;
  532. int rc = 0;
  533. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  534. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  535. printk(KERN_WARNING "%s: Specified number of users is "
  536. "too large, defaulting to [%d] users\n", __func__,
  537. ecryptfs_number_of_users);
  538. }
  539. mutex_init(&ecryptfs_daemon_hash_mux);
  540. mutex_lock(&ecryptfs_daemon_hash_mux);
  541. ecryptfs_hash_buckets = 1;
  542. while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
  543. ecryptfs_hash_buckets++;
  544. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  545. * ecryptfs_hash_buckets), GFP_KERNEL);
  546. if (!ecryptfs_daemon_hash) {
  547. rc = -ENOMEM;
  548. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  549. mutex_unlock(&ecryptfs_daemon_hash_mux);
  550. goto out;
  551. }
  552. for (i = 0; i < ecryptfs_hash_buckets; i++)
  553. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  554. mutex_unlock(&ecryptfs_daemon_hash_mux);
  555. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  556. * ecryptfs_message_buf_len),
  557. GFP_KERNEL);
  558. if (!ecryptfs_msg_ctx_arr) {
  559. rc = -ENOMEM;
  560. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  561. goto out;
  562. }
  563. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  564. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  565. ecryptfs_msg_counter = 0;
  566. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  567. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  568. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  569. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  570. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  571. ecryptfs_msg_ctx_arr[i].index = i;
  572. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  573. ecryptfs_msg_ctx_arr[i].counter = 0;
  574. ecryptfs_msg_ctx_arr[i].task = NULL;
  575. ecryptfs_msg_ctx_arr[i].msg = NULL;
  576. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  577. &ecryptfs_msg_ctx_free_list);
  578. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  579. }
  580. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  581. rc = ecryptfs_init_ecryptfs_miscdev();
  582. if (rc)
  583. ecryptfs_release_messaging();
  584. out:
  585. return rc;
  586. }
  587. void ecryptfs_release_messaging(void)
  588. {
  589. if (ecryptfs_msg_ctx_arr) {
  590. int i;
  591. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  592. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  593. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  594. if (ecryptfs_msg_ctx_arr[i].msg)
  595. kfree(ecryptfs_msg_ctx_arr[i].msg);
  596. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  597. }
  598. kfree(ecryptfs_msg_ctx_arr);
  599. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  600. }
  601. if (ecryptfs_daemon_hash) {
  602. struct hlist_node *elem;
  603. struct ecryptfs_daemon *daemon;
  604. int i;
  605. mutex_lock(&ecryptfs_daemon_hash_mux);
  606. for (i = 0; i < ecryptfs_hash_buckets; i++) {
  607. int rc;
  608. hlist_for_each_entry(daemon, elem,
  609. &ecryptfs_daemon_hash[i],
  610. euid_chain) {
  611. rc = ecryptfs_exorcise_daemon(daemon);
  612. if (rc)
  613. printk(KERN_ERR "%s: Error whilst "
  614. "attempting to destroy daemon; "
  615. "rc = [%d]. Dazed and confused, "
  616. "but trying to continue.\n",
  617. __func__, rc);
  618. }
  619. }
  620. kfree(ecryptfs_daemon_hash);
  621. mutex_unlock(&ecryptfs_daemon_hash_mux);
  622. }
  623. ecryptfs_destroy_ecryptfs_miscdev();
  624. return;
  625. }