messaging.c 20 KB

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