messaging.c 22 KB

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