messaging.c 17 KB

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