messaging.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. kzfree(daemon);
  277. out:
  278. return rc;
  279. }
  280. /**
  281. * ecryptfs_process_quit
  282. * @euid: The user ID owner of the message
  283. * @user_ns: The namespace in which @euid applies
  284. * @pid: The process ID for the userspace program that sent the
  285. * message
  286. *
  287. * Deletes the corresponding daemon for the given euid and pid, if
  288. * it is the registered that is requesting the deletion. Returns zero
  289. * after deleting the desired daemon; non-zero otherwise.
  290. */
  291. int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
  292. struct pid *pid)
  293. {
  294. struct ecryptfs_daemon *daemon;
  295. int rc;
  296. mutex_lock(&ecryptfs_daemon_hash_mux);
  297. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
  298. if (rc || !daemon) {
  299. rc = -EINVAL;
  300. printk(KERN_ERR "Received request from user [%d] to "
  301. "unregister unrecognized daemon [0x%p]\n", euid, pid);
  302. goto out_unlock;
  303. }
  304. rc = ecryptfs_exorcise_daemon(daemon);
  305. out_unlock:
  306. mutex_unlock(&ecryptfs_daemon_hash_mux);
  307. return rc;
  308. }
  309. /**
  310. * ecryptfs_process_reponse
  311. * @msg: The ecryptfs message received; the caller should sanity check
  312. * msg->data_len and free the memory
  313. * @pid: The process ID of the userspace application that sent the
  314. * message
  315. * @seq: The sequence number of the message; must match the sequence
  316. * number for the existing message context waiting for this
  317. * response
  318. *
  319. * Processes a response message after sending an operation request to
  320. * userspace. Some other process is awaiting this response. Before
  321. * sending out its first communications, the other process allocated a
  322. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  323. * response message contains this index so that we can copy over the
  324. * response message into the msg_ctx that the process holds a
  325. * reference to. The other process is going to wake up, check to see
  326. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  327. * proceed to read off and process the response message. Returns zero
  328. * upon delivery to desired context element; non-zero upon delivery
  329. * failure or error.
  330. *
  331. * Returns zero on success; non-zero otherwise
  332. */
  333. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
  334. struct user_namespace *user_ns, struct pid *pid,
  335. u32 seq)
  336. {
  337. struct ecryptfs_daemon *daemon;
  338. struct ecryptfs_msg_ctx *msg_ctx;
  339. size_t msg_size;
  340. struct nsproxy *nsproxy;
  341. struct user_namespace *tsk_user_ns;
  342. uid_t ctx_euid;
  343. int rc;
  344. if (msg->index >= ecryptfs_message_buf_len) {
  345. rc = -EINVAL;
  346. printk(KERN_ERR "%s: Attempt to reference "
  347. "context buffer at index [%d]; maximum "
  348. "allowable is [%d]\n", __func__, msg->index,
  349. (ecryptfs_message_buf_len - 1));
  350. goto out;
  351. }
  352. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  353. mutex_lock(&msg_ctx->mux);
  354. mutex_lock(&ecryptfs_daemon_hash_mux);
  355. rcu_read_lock();
  356. nsproxy = task_nsproxy(msg_ctx->task);
  357. if (nsproxy == NULL) {
  358. rc = -EBADMSG;
  359. printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
  360. "message.\n", __func__);
  361. rcu_read_unlock();
  362. mutex_unlock(&ecryptfs_daemon_hash_mux);
  363. goto wake_up;
  364. }
  365. tsk_user_ns = __task_cred(msg_ctx->task)->user->user_ns;
  366. ctx_euid = task_euid(msg_ctx->task);
  367. rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
  368. rcu_read_unlock();
  369. mutex_unlock(&ecryptfs_daemon_hash_mux);
  370. if (rc) {
  371. rc = -EBADMSG;
  372. printk(KERN_WARNING "%s: User [%d] received a "
  373. "message response from process [0x%p] but does "
  374. "not have a registered daemon\n", __func__,
  375. ctx_euid, pid);
  376. goto wake_up;
  377. }
  378. if (ctx_euid != euid) {
  379. rc = -EBADMSG;
  380. printk(KERN_WARNING "%s: Received message from user "
  381. "[%d]; expected message from user [%d]\n", __func__,
  382. euid, ctx_euid);
  383. goto unlock;
  384. }
  385. if (tsk_user_ns != user_ns) {
  386. rc = -EBADMSG;
  387. printk(KERN_WARNING "%s: Received message from user_ns "
  388. "[0x%p]; expected message from user_ns [0x%p]\n",
  389. __func__, user_ns, tsk_user_ns);
  390. goto unlock;
  391. }
  392. if (daemon->pid != pid) {
  393. rc = -EBADMSG;
  394. printk(KERN_ERR "%s: User [%d] sent a message response "
  395. "from an unrecognized process [0x%p]\n",
  396. __func__, ctx_euid, pid);
  397. goto unlock;
  398. }
  399. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  400. rc = -EINVAL;
  401. printk(KERN_WARNING "%s: Desired context element is not "
  402. "pending a response\n", __func__);
  403. goto unlock;
  404. } else if (msg_ctx->counter != seq) {
  405. rc = -EINVAL;
  406. printk(KERN_WARNING "%s: Invalid message sequence; "
  407. "expected [%d]; received [%d]\n", __func__,
  408. msg_ctx->counter, seq);
  409. goto unlock;
  410. }
  411. msg_size = (sizeof(*msg) + msg->data_len);
  412. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  413. if (!msg_ctx->msg) {
  414. rc = -ENOMEM;
  415. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  416. "GFP_KERNEL memory\n", __func__, msg_size);
  417. goto unlock;
  418. }
  419. memcpy(msg_ctx->msg, msg, msg_size);
  420. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  421. rc = 0;
  422. wake_up:
  423. wake_up_process(msg_ctx->task);
  424. unlock:
  425. mutex_unlock(&msg_ctx->mux);
  426. out:
  427. return rc;
  428. }
  429. /**
  430. * ecryptfs_send_message_locked
  431. * @data: The data to send
  432. * @data_len: The length of data
  433. * @msg_ctx: The message context allocated for the send
  434. *
  435. * Must be called with ecryptfs_daemon_hash_mux held.
  436. *
  437. * Returns zero on success; non-zero otherwise
  438. */
  439. static int
  440. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  441. struct ecryptfs_msg_ctx **msg_ctx)
  442. {
  443. struct ecryptfs_daemon *daemon;
  444. uid_t euid = current_euid();
  445. int rc;
  446. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  447. if (rc || !daemon) {
  448. rc = -ENOTCONN;
  449. printk(KERN_ERR "%s: User [%d] does not have a daemon "
  450. "registered\n", __func__, euid);
  451. goto out;
  452. }
  453. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  454. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  455. if (rc) {
  456. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  457. printk(KERN_WARNING "%s: Could not claim a free "
  458. "context element\n", __func__);
  459. goto out;
  460. }
  461. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  462. mutex_unlock(&(*msg_ctx)->mux);
  463. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  464. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  465. daemon);
  466. if (rc)
  467. printk(KERN_ERR "%s: Error attempting to send message to "
  468. "userspace daemon; rc = [%d]\n", __func__, rc);
  469. out:
  470. return rc;
  471. }
  472. /**
  473. * ecryptfs_send_message
  474. * @data: The data to send
  475. * @data_len: The length of data
  476. * @msg_ctx: The message context allocated for the send
  477. *
  478. * Grabs ecryptfs_daemon_hash_mux.
  479. *
  480. * Returns zero on success; non-zero otherwise
  481. */
  482. int ecryptfs_send_message(char *data, int data_len,
  483. struct ecryptfs_msg_ctx **msg_ctx)
  484. {
  485. int rc;
  486. mutex_lock(&ecryptfs_daemon_hash_mux);
  487. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  488. msg_ctx);
  489. mutex_unlock(&ecryptfs_daemon_hash_mux);
  490. return rc;
  491. }
  492. /**
  493. * ecryptfs_wait_for_response
  494. * @msg_ctx: The context that was assigned when sending a message
  495. * @msg: The incoming message from userspace; not set if rc != 0
  496. *
  497. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  498. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  499. * returned, msg will point to a valid message from userspace; a
  500. * non-zero value is returned upon failure to receive a message or an
  501. * error occurs. Callee must free @msg on success.
  502. */
  503. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  504. struct ecryptfs_message **msg)
  505. {
  506. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  507. int rc = 0;
  508. sleep:
  509. timeout = schedule_timeout_interruptible(timeout);
  510. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  511. mutex_lock(&msg_ctx->mux);
  512. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  513. if (timeout) {
  514. mutex_unlock(&msg_ctx->mux);
  515. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  516. goto sleep;
  517. }
  518. rc = -ENOMSG;
  519. } else {
  520. *msg = msg_ctx->msg;
  521. msg_ctx->msg = NULL;
  522. }
  523. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  524. mutex_unlock(&msg_ctx->mux);
  525. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  526. return rc;
  527. }
  528. int ecryptfs_init_messaging(void)
  529. {
  530. int i;
  531. int rc = 0;
  532. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  533. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  534. printk(KERN_WARNING "%s: Specified number of users is "
  535. "too large, defaulting to [%d] users\n", __func__,
  536. ecryptfs_number_of_users);
  537. }
  538. mutex_init(&ecryptfs_daemon_hash_mux);
  539. mutex_lock(&ecryptfs_daemon_hash_mux);
  540. ecryptfs_hash_buckets = 1;
  541. while (ecryptfs_number_of_users >> ecryptfs_hash_buckets)
  542. ecryptfs_hash_buckets++;
  543. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  544. * ecryptfs_hash_buckets), GFP_KERNEL);
  545. if (!ecryptfs_daemon_hash) {
  546. rc = -ENOMEM;
  547. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  548. mutex_unlock(&ecryptfs_daemon_hash_mux);
  549. goto out;
  550. }
  551. for (i = 0; i < ecryptfs_hash_buckets; i++)
  552. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  553. mutex_unlock(&ecryptfs_daemon_hash_mux);
  554. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  555. * ecryptfs_message_buf_len),
  556. GFP_KERNEL);
  557. if (!ecryptfs_msg_ctx_arr) {
  558. rc = -ENOMEM;
  559. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  560. goto out;
  561. }
  562. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  563. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  564. ecryptfs_msg_counter = 0;
  565. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  566. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  567. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  568. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  569. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  570. ecryptfs_msg_ctx_arr[i].index = i;
  571. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  572. ecryptfs_msg_ctx_arr[i].counter = 0;
  573. ecryptfs_msg_ctx_arr[i].task = NULL;
  574. ecryptfs_msg_ctx_arr[i].msg = NULL;
  575. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  576. &ecryptfs_msg_ctx_free_list);
  577. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  578. }
  579. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  580. rc = ecryptfs_init_ecryptfs_miscdev();
  581. if (rc)
  582. ecryptfs_release_messaging();
  583. out:
  584. return rc;
  585. }
  586. void ecryptfs_release_messaging(void)
  587. {
  588. if (ecryptfs_msg_ctx_arr) {
  589. int i;
  590. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  591. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  592. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  593. if (ecryptfs_msg_ctx_arr[i].msg)
  594. kfree(ecryptfs_msg_ctx_arr[i].msg);
  595. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  596. }
  597. kfree(ecryptfs_msg_ctx_arr);
  598. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  599. }
  600. if (ecryptfs_daemon_hash) {
  601. struct hlist_node *elem;
  602. struct ecryptfs_daemon *daemon;
  603. int i;
  604. mutex_lock(&ecryptfs_daemon_hash_mux);
  605. for (i = 0; i < ecryptfs_hash_buckets; i++) {
  606. int rc;
  607. hlist_for_each_entry(daemon, elem,
  608. &ecryptfs_daemon_hash[i],
  609. euid_chain) {
  610. rc = ecryptfs_exorcise_daemon(daemon);
  611. if (rc)
  612. printk(KERN_ERR "%s: Error whilst "
  613. "attempting to destroy daemon; "
  614. "rc = [%d]. Dazed and confused, "
  615. "but trying to continue.\n",
  616. __func__, rc);
  617. }
  618. }
  619. kfree(ecryptfs_daemon_hash);
  620. mutex_unlock(&ecryptfs_daemon_hash_mux);
  621. }
  622. ecryptfs_destroy_ecryptfs_miscdev();
  623. return;
  624. }