messaging.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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_reponse
  206. * @msg: The ecryptfs message received; the caller should sanity check
  207. * msg->data_len and free the memory
  208. * @pid: The process ID of the userspace application that sent the
  209. * message
  210. * @seq: The sequence number of the message; must match the sequence
  211. * number for the existing message context waiting for this
  212. * response
  213. *
  214. * Processes a response message after sending an operation request to
  215. * userspace. Some other process is awaiting this response. Before
  216. * sending out its first communications, the other process allocated a
  217. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  218. * response message contains this index so that we can copy over the
  219. * response message into the msg_ctx that the process holds a
  220. * reference to. The other process is going to wake up, check to see
  221. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  222. * proceed to read off and process the response message. Returns zero
  223. * upon delivery to desired context element; non-zero upon delivery
  224. * failure or error.
  225. *
  226. * Returns zero on success; non-zero otherwise
  227. */
  228. int ecryptfs_process_response(struct ecryptfs_message *msg, uid_t euid,
  229. struct user_namespace *user_ns, struct pid *pid,
  230. u32 seq)
  231. {
  232. struct ecryptfs_daemon *uninitialized_var(daemon);
  233. struct ecryptfs_msg_ctx *msg_ctx;
  234. size_t msg_size;
  235. struct nsproxy *nsproxy;
  236. struct user_namespace *tsk_user_ns;
  237. uid_t ctx_euid;
  238. int rc;
  239. if (msg->index >= ecryptfs_message_buf_len) {
  240. rc = -EINVAL;
  241. printk(KERN_ERR "%s: Attempt to reference "
  242. "context buffer at index [%d]; maximum "
  243. "allowable is [%d]\n", __func__, msg->index,
  244. (ecryptfs_message_buf_len - 1));
  245. goto out;
  246. }
  247. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  248. mutex_lock(&msg_ctx->mux);
  249. mutex_lock(&ecryptfs_daemon_hash_mux);
  250. rcu_read_lock();
  251. nsproxy = task_nsproxy(msg_ctx->task);
  252. if (nsproxy == NULL) {
  253. rc = -EBADMSG;
  254. printk(KERN_ERR "%s: Receiving process is a zombie. Dropping "
  255. "message.\n", __func__);
  256. rcu_read_unlock();
  257. mutex_unlock(&ecryptfs_daemon_hash_mux);
  258. goto wake_up;
  259. }
  260. tsk_user_ns = __task_cred(msg_ctx->task)->user_ns;
  261. ctx_euid = task_euid(msg_ctx->task);
  262. rc = ecryptfs_find_daemon_by_euid(&daemon, ctx_euid, tsk_user_ns);
  263. rcu_read_unlock();
  264. mutex_unlock(&ecryptfs_daemon_hash_mux);
  265. if (rc) {
  266. rc = -EBADMSG;
  267. printk(KERN_WARNING "%s: User [%d] received a "
  268. "message response from process [0x%p] but does "
  269. "not have a registered daemon\n", __func__,
  270. ctx_euid, pid);
  271. goto wake_up;
  272. }
  273. if (ctx_euid != euid) {
  274. rc = -EBADMSG;
  275. printk(KERN_WARNING "%s: Received message from user "
  276. "[%d]; expected message from user [%d]\n", __func__,
  277. euid, ctx_euid);
  278. goto unlock;
  279. }
  280. if (tsk_user_ns != user_ns) {
  281. rc = -EBADMSG;
  282. printk(KERN_WARNING "%s: Received message from user_ns "
  283. "[0x%p]; expected message from user_ns [0x%p]\n",
  284. __func__, user_ns, tsk_user_ns);
  285. goto unlock;
  286. }
  287. if (daemon->pid != pid) {
  288. rc = -EBADMSG;
  289. printk(KERN_ERR "%s: User [%d] sent a message response "
  290. "from an unrecognized process [0x%p]\n",
  291. __func__, ctx_euid, pid);
  292. goto unlock;
  293. }
  294. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  295. rc = -EINVAL;
  296. printk(KERN_WARNING "%s: Desired context element is not "
  297. "pending a response\n", __func__);
  298. goto unlock;
  299. } else if (msg_ctx->counter != seq) {
  300. rc = -EINVAL;
  301. printk(KERN_WARNING "%s: Invalid message sequence; "
  302. "expected [%d]; received [%d]\n", __func__,
  303. msg_ctx->counter, seq);
  304. goto unlock;
  305. }
  306. msg_size = (sizeof(*msg) + msg->data_len);
  307. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  308. if (!msg_ctx->msg) {
  309. rc = -ENOMEM;
  310. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  311. "GFP_KERNEL memory\n", __func__, msg_size);
  312. goto unlock;
  313. }
  314. memcpy(msg_ctx->msg, msg, msg_size);
  315. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  316. rc = 0;
  317. wake_up:
  318. wake_up_process(msg_ctx->task);
  319. unlock:
  320. mutex_unlock(&msg_ctx->mux);
  321. out:
  322. return rc;
  323. }
  324. /**
  325. * ecryptfs_send_message_locked
  326. * @data: The data to send
  327. * @data_len: The length of data
  328. * @msg_ctx: The message context allocated for the send
  329. *
  330. * Must be called with ecryptfs_daemon_hash_mux held.
  331. *
  332. * Returns zero on success; non-zero otherwise
  333. */
  334. static int
  335. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  336. struct ecryptfs_msg_ctx **msg_ctx)
  337. {
  338. struct ecryptfs_daemon *daemon;
  339. uid_t euid = current_euid();
  340. int rc;
  341. rc = ecryptfs_find_daemon_by_euid(&daemon, euid, current_user_ns());
  342. if (rc || !daemon) {
  343. rc = -ENOTCONN;
  344. printk(KERN_ERR "%s: User [%d] does not have a daemon "
  345. "registered\n", __func__, euid);
  346. goto out;
  347. }
  348. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  349. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  350. if (rc) {
  351. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  352. printk(KERN_WARNING "%s: Could not claim a free "
  353. "context element\n", __func__);
  354. goto out;
  355. }
  356. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  357. mutex_unlock(&(*msg_ctx)->mux);
  358. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  359. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  360. daemon);
  361. if (rc)
  362. printk(KERN_ERR "%s: Error attempting to send message to "
  363. "userspace daemon; rc = [%d]\n", __func__, rc);
  364. out:
  365. return rc;
  366. }
  367. /**
  368. * ecryptfs_send_message
  369. * @data: The data to send
  370. * @data_len: The length of data
  371. * @msg_ctx: The message context allocated for the send
  372. *
  373. * Grabs ecryptfs_daemon_hash_mux.
  374. *
  375. * Returns zero on success; non-zero otherwise
  376. */
  377. int ecryptfs_send_message(char *data, int data_len,
  378. struct ecryptfs_msg_ctx **msg_ctx)
  379. {
  380. int rc;
  381. mutex_lock(&ecryptfs_daemon_hash_mux);
  382. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  383. msg_ctx);
  384. mutex_unlock(&ecryptfs_daemon_hash_mux);
  385. return rc;
  386. }
  387. /**
  388. * ecryptfs_wait_for_response
  389. * @msg_ctx: The context that was assigned when sending a message
  390. * @msg: The incoming message from userspace; not set if rc != 0
  391. *
  392. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  393. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  394. * returned, msg will point to a valid message from userspace; a
  395. * non-zero value is returned upon failure to receive a message or an
  396. * error occurs. Callee must free @msg on success.
  397. */
  398. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  399. struct ecryptfs_message **msg)
  400. {
  401. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  402. int rc = 0;
  403. sleep:
  404. timeout = schedule_timeout_interruptible(timeout);
  405. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  406. mutex_lock(&msg_ctx->mux);
  407. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  408. if (timeout) {
  409. mutex_unlock(&msg_ctx->mux);
  410. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  411. goto sleep;
  412. }
  413. rc = -ENOMSG;
  414. } else {
  415. *msg = msg_ctx->msg;
  416. msg_ctx->msg = NULL;
  417. }
  418. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  419. mutex_unlock(&msg_ctx->mux);
  420. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  421. return rc;
  422. }
  423. int __init ecryptfs_init_messaging(void)
  424. {
  425. int i;
  426. int rc = 0;
  427. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  428. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  429. printk(KERN_WARNING "%s: Specified number of users is "
  430. "too large, defaulting to [%d] users\n", __func__,
  431. ecryptfs_number_of_users);
  432. }
  433. mutex_init(&ecryptfs_daemon_hash_mux);
  434. mutex_lock(&ecryptfs_daemon_hash_mux);
  435. ecryptfs_hash_bits = 1;
  436. while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
  437. ecryptfs_hash_bits++;
  438. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  439. * (1 << ecryptfs_hash_bits)),
  440. GFP_KERNEL);
  441. if (!ecryptfs_daemon_hash) {
  442. rc = -ENOMEM;
  443. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  444. mutex_unlock(&ecryptfs_daemon_hash_mux);
  445. goto out;
  446. }
  447. for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
  448. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  449. mutex_unlock(&ecryptfs_daemon_hash_mux);
  450. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  451. * ecryptfs_message_buf_len),
  452. GFP_KERNEL);
  453. if (!ecryptfs_msg_ctx_arr) {
  454. rc = -ENOMEM;
  455. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  456. goto out;
  457. }
  458. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  459. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  460. ecryptfs_msg_counter = 0;
  461. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  462. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  463. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  464. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  465. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  466. ecryptfs_msg_ctx_arr[i].index = i;
  467. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  468. ecryptfs_msg_ctx_arr[i].counter = 0;
  469. ecryptfs_msg_ctx_arr[i].task = NULL;
  470. ecryptfs_msg_ctx_arr[i].msg = NULL;
  471. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  472. &ecryptfs_msg_ctx_free_list);
  473. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  474. }
  475. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  476. rc = ecryptfs_init_ecryptfs_miscdev();
  477. if (rc)
  478. ecryptfs_release_messaging();
  479. out:
  480. return rc;
  481. }
  482. void ecryptfs_release_messaging(void)
  483. {
  484. if (ecryptfs_msg_ctx_arr) {
  485. int i;
  486. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  487. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  488. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  489. if (ecryptfs_msg_ctx_arr[i].msg)
  490. kfree(ecryptfs_msg_ctx_arr[i].msg);
  491. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  492. }
  493. kfree(ecryptfs_msg_ctx_arr);
  494. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  495. }
  496. if (ecryptfs_daemon_hash) {
  497. struct hlist_node *elem;
  498. struct ecryptfs_daemon *daemon;
  499. int i;
  500. mutex_lock(&ecryptfs_daemon_hash_mux);
  501. for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
  502. int rc;
  503. hlist_for_each_entry(daemon, elem,
  504. &ecryptfs_daemon_hash[i],
  505. euid_chain) {
  506. rc = ecryptfs_exorcise_daemon(daemon);
  507. if (rc)
  508. printk(KERN_ERR "%s: Error whilst "
  509. "attempting to destroy daemon; "
  510. "rc = [%d]. Dazed and confused, "
  511. "but trying to continue.\n",
  512. __func__, rc);
  513. }
  514. }
  515. kfree(ecryptfs_daemon_hash);
  516. mutex_unlock(&ecryptfs_daemon_hash_mux);
  517. }
  518. ecryptfs_destroy_ecryptfs_miscdev();
  519. return;
  520. }