messaging.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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/slab.h>
  24. #include <linux/user_namespace.h>
  25. #include <linux/nsproxy.h>
  26. #include "ecryptfs_kernel.h"
  27. static LIST_HEAD(ecryptfs_msg_ctx_free_list);
  28. static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
  29. static struct mutex ecryptfs_msg_ctx_lists_mux;
  30. static struct hlist_head *ecryptfs_daemon_hash;
  31. struct mutex ecryptfs_daemon_hash_mux;
  32. static int ecryptfs_hash_bits;
  33. #define ecryptfs_uid_hash(uid) \
  34. hash_long((unsigned long)uid, ecryptfs_hash_bits)
  35. static u32 ecryptfs_msg_counter;
  36. static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
  37. /**
  38. * ecryptfs_acquire_free_msg_ctx
  39. * @msg_ctx: The context that was acquired from the free list
  40. *
  41. * Acquires a context element from the free list and locks the mutex
  42. * on the context. Sets the msg_ctx task to current. Returns zero on
  43. * success; non-zero on error or upon failure to acquire a free
  44. * context element. Must be called with ecryptfs_msg_ctx_lists_mux
  45. * held.
  46. */
  47. static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
  48. {
  49. struct list_head *p;
  50. int rc;
  51. if (list_empty(&ecryptfs_msg_ctx_free_list)) {
  52. printk(KERN_WARNING "%s: The eCryptfs free "
  53. "context list is empty. It may be helpful to "
  54. "specify the ecryptfs_message_buf_len "
  55. "parameter to be greater than the current "
  56. "value of [%d]\n", __func__, ecryptfs_message_buf_len);
  57. rc = -ENOMEM;
  58. goto out;
  59. }
  60. list_for_each(p, &ecryptfs_msg_ctx_free_list) {
  61. *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
  62. if (mutex_trylock(&(*msg_ctx)->mux)) {
  63. (*msg_ctx)->task = current;
  64. rc = 0;
  65. goto out;
  66. }
  67. }
  68. rc = -ENOMEM;
  69. out:
  70. return rc;
  71. }
  72. /**
  73. * ecryptfs_msg_ctx_free_to_alloc
  74. * @msg_ctx: The context to move from the free list to the alloc list
  75. *
  76. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  77. */
  78. static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
  79. {
  80. list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
  81. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
  82. msg_ctx->counter = ++ecryptfs_msg_counter;
  83. }
  84. /**
  85. * ecryptfs_msg_ctx_alloc_to_free
  86. * @msg_ctx: The context to move from the alloc list to the free list
  87. *
  88. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  89. */
  90. void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
  91. {
  92. list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
  93. if (msg_ctx->msg)
  94. kfree(msg_ctx->msg);
  95. msg_ctx->msg = NULL;
  96. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
  97. }
  98. /**
  99. * ecryptfs_find_daemon_by_euid
  100. * @euid: The effective user id which maps to the desired daemon id
  101. * @user_ns: The namespace in which @euid applies
  102. * @daemon: If return value is zero, points to the desired daemon pointer
  103. *
  104. * Must be called with ecryptfs_daemon_hash_mux held.
  105. *
  106. * Search the hash list for the given user id.
  107. *
  108. * Returns zero if the user id exists in the list; non-zero otherwise.
  109. */
  110. int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon, uid_t euid,
  111. struct user_namespace *user_ns)
  112. {
  113. struct hlist_node *elem;
  114. int rc;
  115. hlist_for_each_entry(*daemon, elem,
  116. &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)],
  117. euid_chain) {
  118. if ((*daemon)->euid == euid && (*daemon)->user_ns == user_ns) {
  119. rc = 0;
  120. goto out;
  121. }
  122. }
  123. rc = -EINVAL;
  124. out:
  125. return rc;
  126. }
  127. /**
  128. * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
  129. * @daemon: Pointer to set to newly allocated daemon struct
  130. * @euid: Effective user id for the daemon
  131. * @user_ns: The namespace in which @euid applies
  132. * @pid: Process id for the daemon
  133. *
  134. * Must be called ceremoniously while in possession of
  135. * ecryptfs_sacred_daemon_hash_mux
  136. *
  137. * Returns zero on success; non-zero otherwise
  138. */
  139. int
  140. ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, uid_t euid,
  141. struct user_namespace *user_ns, struct pid *pid)
  142. {
  143. int rc = 0;
  144. (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
  145. if (!(*daemon)) {
  146. rc = -ENOMEM;
  147. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  148. "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
  149. goto out;
  150. }
  151. (*daemon)->euid = euid;
  152. (*daemon)->user_ns = get_user_ns(user_ns);
  153. (*daemon)->pid = get_pid(pid);
  154. (*daemon)->task = current;
  155. mutex_init(&(*daemon)->mux);
  156. INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
  157. init_waitqueue_head(&(*daemon)->wait);
  158. (*daemon)->num_queued_msg_ctx = 0;
  159. hlist_add_head(&(*daemon)->euid_chain,
  160. &ecryptfs_daemon_hash[ecryptfs_uid_hash(euid)]);
  161. out:
  162. return rc;
  163. }
  164. /**
  165. * ecryptfs_exorcise_daemon - Destroy the daemon struct
  166. *
  167. * Must be called ceremoniously while in possession of
  168. * ecryptfs_daemon_hash_mux and the daemon's own mux.
  169. */
  170. int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
  171. {
  172. struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
  173. int rc = 0;
  174. mutex_lock(&daemon->mux);
  175. if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  176. || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
  177. rc = -EBUSY;
  178. printk(KERN_WARNING "%s: Attempt to destroy daemon with pid "
  179. "[0x%p], but it is in the midst of a read or a poll\n",
  180. __func__, daemon->pid);
  181. mutex_unlock(&daemon->mux);
  182. goto out;
  183. }
  184. list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
  185. &daemon->msg_ctx_out_queue, daemon_out_list) {
  186. list_del(&msg_ctx->daemon_out_list);
  187. daemon->num_queued_msg_ctx--;
  188. printk(KERN_WARNING "%s: Warning: dropping message that is in "
  189. "the out queue of a dying daemon\n", __func__);
  190. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  191. }
  192. hlist_del(&daemon->euid_chain);
  193. if (daemon->task)
  194. wake_up_process(daemon->task);
  195. if (daemon->pid)
  196. put_pid(daemon->pid);
  197. if (daemon->user_ns)
  198. put_user_ns(daemon->user_ns);
  199. mutex_unlock(&daemon->mux);
  200. kzfree(daemon);
  201. out:
  202. return rc;
  203. }
  204. /**
  205. * ecryptfs_process_quit
  206. * @euid: The user ID owner of the message
  207. * @user_ns: The namespace in which @euid applies
  208. * @pid: The process ID for the userspace program that sent the
  209. * message
  210. *
  211. * Deletes the corresponding daemon for the given euid and pid, if
  212. * it is the registered that is requesting the deletion. Returns zero
  213. * after deleting the desired daemon; non-zero otherwise.
  214. */
  215. int ecryptfs_process_quit(uid_t euid, struct user_namespace *user_ns,
  216. struct pid *pid)
  217. {
  218. struct ecryptfs_daemon *daemon;
  219. int rc;
  220. mutex_lock(&ecryptfs_daemon_hash_mux);
  221. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, user_ns);
  222. if (rc || !daemon) {
  223. rc = -EINVAL;
  224. printk(KERN_ERR "Received request from user [%d] to "
  225. "unregister unrecognized daemon [0x%p]\n", euid, pid);
  226. goto out_unlock;
  227. }
  228. rc = ecryptfs_exorcise_daemon(daemon);
  229. out_unlock:
  230. mutex_unlock(&ecryptfs_daemon_hash_mux);
  231. return rc;
  232. }
  233. /**
  234. * ecryptfs_process_reponse
  235. * @msg: The ecryptfs message received; the caller should sanity check
  236. * msg->data_len and free the memory
  237. * @pid: The process ID of the userspace application that sent the
  238. * message
  239. * @seq: The sequence number of the message; must match the sequence
  240. * number for the existing message context waiting for this
  241. * response
  242. *
  243. * Processes a response message after sending an operation request to
  244. * userspace. Some other process is awaiting this response. Before
  245. * sending out its first communications, the other process allocated a
  246. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  247. * response message contains this index so that we can copy over the
  248. * response message into the msg_ctx that the process holds a
  249. * reference to. The other process is going to wake up, check to see
  250. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  251. * proceed to read off and process the response message. Returns zero
  252. * upon delivery to desired context element; non-zero upon delivery
  253. * failure or error.
  254. *
  255. * Returns zero on success; non-zero otherwise
  256. */
  257. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
  258. struct user_namespace *user_ns, struct pid *pid,
  259. u32 seq)
  260. {
  261. struct ecryptfs_daemon *uninitialized_var(daemon);
  262. struct ecryptfs_msg_ctx *msg_ctx;
  263. size_t msg_size;
  264. struct nsproxy *nsproxy;
  265. struct user_namespace *tsk_user_ns;
  266. uid_t ctx_euid;
  267. int rc;
  268. if (msg->index >= ecryptfs_message_buf_len) {
  269. rc = -EINVAL;
  270. printk(KERN_ERR "%s: Attempt to reference "
  271. "context buffer at index [%d]; maximum "
  272. "allowable is [%d]\n", __func__, msg->index,
  273. (ecryptfs_message_buf_len - 1));
  274. goto out;
  275. }
  276. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  277. mutex_lock(&msg_ctx->mux);
  278. mutex_lock(&ecryptfs_daemon_hash_mux);
  279. task_lock(msg_ctx->task);
  280. nsproxy = msg_ctx->task->nsproxy;
  281. if (nsproxy == NULL) {
  282. rc = -EBADMSG;
  283. printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
  284. "message.\n", __func__);
  285. task_unlock(msg_ctx->task);
  286. mutex_unlock(&ecryptfs_daemon_hash_mux);
  287. goto wake_up;
  288. }
  289. tsk_user_ns = __task_cred(msg_ctx->task)->user_ns;
  290. ctx_euid = task_euid(msg_ctx->task);
  291. rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
  292. task_unlock(msg_ctx->task);
  293. mutex_unlock(&ecryptfs_daemon_hash_mux);
  294. if (rc) {
  295. rc = -EBADMSG;
  296. printk(KERN_WARNING "%s: User [%d] received a "
  297. "message response from process [0x%p] but does "
  298. "not have a registered daemon\n", __func__,
  299. ctx_euid, pid);
  300. goto wake_up;
  301. }
  302. if (ctx_euid != euid) {
  303. rc = -EBADMSG;
  304. printk(KERN_WARNING "%s: Received message from user "
  305. "[%d]; expected message from user [%d]\n", __func__,
  306. euid, ctx_euid);
  307. goto unlock;
  308. }
  309. if (tsk_user_ns != user_ns) {
  310. rc = -EBADMSG;
  311. printk(KERN_WARNING "%s: Received message from user_ns "
  312. "[0x%p]; expected message from user_ns [0x%p]\n",
  313. __func__, user_ns, tsk_user_ns);
  314. goto unlock;
  315. }
  316. if (daemon->pid != pid) {
  317. rc = -EBADMSG;
  318. printk(KERN_ERR "%s: User [%d] sent a message response "
  319. "from an unrecognized process [0x%p]\n",
  320. __func__, ctx_euid, pid);
  321. goto unlock;
  322. }
  323. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  324. rc = -EINVAL;
  325. printk(KERN_WARNING "%s: Desired context element is not "
  326. "pending a response\n", __func__);
  327. goto unlock;
  328. } else if (msg_ctx->counter != seq) {
  329. rc = -EINVAL;
  330. printk(KERN_WARNING "%s: Invalid message sequence; "
  331. "expected [%d]; received [%d]\n", __func__,
  332. msg_ctx->counter, seq);
  333. goto unlock;
  334. }
  335. msg_size = (sizeof(*msg) + msg->data_len);
  336. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  337. if (!msg_ctx->msg) {
  338. rc = -ENOMEM;
  339. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  340. "GFP_KERNEL memory\n", __func__, msg_size);
  341. goto unlock;
  342. }
  343. memcpy(msg_ctx->msg, msg, msg_size);
  344. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  345. rc = 0;
  346. wake_up:
  347. wake_up_process(msg_ctx->task);
  348. unlock:
  349. mutex_unlock(&msg_ctx->mux);
  350. out:
  351. return rc;
  352. }
  353. /**
  354. * ecryptfs_send_message_locked
  355. * @data: The data to send
  356. * @data_len: The length of data
  357. * @msg_ctx: The message context allocated for the send
  358. *
  359. * Must be called with ecryptfs_daemon_hash_mux held.
  360. *
  361. * Returns zero on success; non-zero otherwise
  362. */
  363. static int
  364. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  365. struct ecryptfs_msg_ctx **msg_ctx)
  366. {
  367. struct ecryptfs_daemon *daemon;
  368. uid_t euid = current_euid();
  369. int rc;
  370. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  371. if (rc || !daemon) {
  372. rc = -ENOTCONN;
  373. printk(KERN_ERR "%s: User [%d] does not have a daemon "
  374. "registered\n", __func__, euid);
  375. goto out;
  376. }
  377. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  378. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  379. if (rc) {
  380. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  381. printk(KERN_WARNING "%s: Could not claim a free "
  382. "context element\n", __func__);
  383. goto out;
  384. }
  385. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  386. mutex_unlock(&(*msg_ctx)->mux);
  387. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  388. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  389. daemon);
  390. if (rc)
  391. printk(KERN_ERR "%s: Error attempting to send message to "
  392. "userspace daemon; rc = [%d]\n", __func__, rc);
  393. out:
  394. return rc;
  395. }
  396. /**
  397. * ecryptfs_send_message
  398. * @data: The data to send
  399. * @data_len: The length of data
  400. * @msg_ctx: The message context allocated for the send
  401. *
  402. * Grabs ecryptfs_daemon_hash_mux.
  403. *
  404. * Returns zero on success; non-zero otherwise
  405. */
  406. int ecryptfs_send_message(char *data, int data_len,
  407. struct ecryptfs_msg_ctx **msg_ctx)
  408. {
  409. int rc;
  410. mutex_lock(&ecryptfs_daemon_hash_mux);
  411. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  412. msg_ctx);
  413. mutex_unlock(&ecryptfs_daemon_hash_mux);
  414. return rc;
  415. }
  416. /**
  417. * ecryptfs_wait_for_response
  418. * @msg_ctx: The context that was assigned when sending a message
  419. * @msg: The incoming message from userspace; not set if rc != 0
  420. *
  421. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  422. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  423. * returned, msg will point to a valid message from userspace; a
  424. * non-zero value is returned upon failure to receive a message or an
  425. * error occurs. Callee must free @msg on success.
  426. */
  427. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  428. struct ecryptfs_message **msg)
  429. {
  430. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  431. int rc = 0;
  432. sleep:
  433. timeout = schedule_timeout_interruptible(timeout);
  434. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  435. mutex_lock(&msg_ctx->mux);
  436. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  437. if (timeout) {
  438. mutex_unlock(&msg_ctx->mux);
  439. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  440. goto sleep;
  441. }
  442. rc = -ENOMSG;
  443. } else {
  444. *msg = msg_ctx->msg;
  445. msg_ctx->msg = NULL;
  446. }
  447. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  448. mutex_unlock(&msg_ctx->mux);
  449. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  450. return rc;
  451. }
  452. int __init ecryptfs_init_messaging(void)
  453. {
  454. int i;
  455. int rc = 0;
  456. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  457. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  458. printk(KERN_WARNING "%s: Specified number of users is "
  459. "too large, defaulting to [%d] users\n", __func__,
  460. ecryptfs_number_of_users);
  461. }
  462. mutex_init(&ecryptfs_daemon_hash_mux);
  463. mutex_lock(&ecryptfs_daemon_hash_mux);
  464. ecryptfs_hash_bits = 1;
  465. while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
  466. ecryptfs_hash_bits++;
  467. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  468. * (1 << ecryptfs_hash_bits)),
  469. GFP_KERNEL);
  470. if (!ecryptfs_daemon_hash) {
  471. rc = -ENOMEM;
  472. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  473. mutex_unlock(&ecryptfs_daemon_hash_mux);
  474. goto out;
  475. }
  476. for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
  477. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  478. mutex_unlock(&ecryptfs_daemon_hash_mux);
  479. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  480. * ecryptfs_message_buf_len),
  481. GFP_KERNEL);
  482. if (!ecryptfs_msg_ctx_arr) {
  483. rc = -ENOMEM;
  484. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  485. goto out;
  486. }
  487. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  488. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  489. ecryptfs_msg_counter = 0;
  490. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  491. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  492. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  493. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  494. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  495. ecryptfs_msg_ctx_arr[i].index = i;
  496. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  497. ecryptfs_msg_ctx_arr[i].counter = 0;
  498. ecryptfs_msg_ctx_arr[i].task = NULL;
  499. ecryptfs_msg_ctx_arr[i].msg = NULL;
  500. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  501. &ecryptfs_msg_ctx_free_list);
  502. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  503. }
  504. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  505. rc = ecryptfs_init_ecryptfs_miscdev();
  506. if (rc)
  507. ecryptfs_release_messaging();
  508. out:
  509. return rc;
  510. }
  511. void ecryptfs_release_messaging(void)
  512. {
  513. if (ecryptfs_msg_ctx_arr) {
  514. int i;
  515. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  516. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  517. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  518. if (ecryptfs_msg_ctx_arr[i].msg)
  519. kfree(ecryptfs_msg_ctx_arr[i].msg);
  520. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  521. }
  522. kfree(ecryptfs_msg_ctx_arr);
  523. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  524. }
  525. if (ecryptfs_daemon_hash) {
  526. struct hlist_node *elem;
  527. struct ecryptfs_daemon *daemon;
  528. int i;
  529. mutex_lock(&ecryptfs_daemon_hash_mux);
  530. for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
  531. int rc;
  532. hlist_for_each_entry(daemon, elem,
  533. &ecryptfs_daemon_hash[i],
  534. euid_chain) {
  535. rc = ecryptfs_exorcise_daemon(daemon);
  536. if (rc)
  537. printk(KERN_ERR "%s: Error whilst "
  538. "attempting to destroy daemon; "
  539. "rc = [%d]. Dazed and confused, "
  540. "but trying to continue.\n",
  541. __func__, rc);
  542. }
  543. }
  544. kfree(ecryptfs_daemon_hash);
  545. mutex_unlock(&ecryptfs_daemon_hash_mux);
  546. }
  547. ecryptfs_destroy_ecryptfs_miscdev();
  548. return;
  549. }