messaging.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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_current_euid_hash(uid) \
  34. hash_long((unsigned long)current_euid(), 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. * @daemon: If return value is zero, points to the desired daemon pointer
  101. *
  102. * Must be called with ecryptfs_daemon_hash_mux held.
  103. *
  104. * Search the hash list for the current effective user id.
  105. *
  106. * Returns zero if the user id exists in the list; non-zero otherwise.
  107. */
  108. int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
  109. {
  110. struct hlist_node *elem;
  111. int rc;
  112. hlist_for_each_entry(*daemon, elem,
  113. &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
  114. euid_chain) {
  115. if ((*daemon)->file->f_cred->euid == current_euid() &&
  116. (*daemon)->file->f_cred->user_ns == current_user_ns()) {
  117. rc = 0;
  118. goto out;
  119. }
  120. }
  121. rc = -EINVAL;
  122. out:
  123. return rc;
  124. }
  125. /**
  126. * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
  127. * @daemon: Pointer to set to newly allocated daemon struct
  128. * @file: File used when opening /dev/ecryptfs
  129. *
  130. * Must be called ceremoniously while in possession of
  131. * ecryptfs_sacred_daemon_hash_mux
  132. *
  133. * Returns zero on success; non-zero otherwise
  134. */
  135. int
  136. ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
  137. {
  138. int rc = 0;
  139. (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
  140. if (!(*daemon)) {
  141. rc = -ENOMEM;
  142. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  143. "GFP_KERNEL memory\n", __func__, sizeof(**daemon));
  144. goto out;
  145. }
  146. (*daemon)->file = file;
  147. mutex_init(&(*daemon)->mux);
  148. INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
  149. init_waitqueue_head(&(*daemon)->wait);
  150. (*daemon)->num_queued_msg_ctx = 0;
  151. hlist_add_head(&(*daemon)->euid_chain,
  152. &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
  153. out:
  154. return rc;
  155. }
  156. /**
  157. * ecryptfs_exorcise_daemon - Destroy the daemon struct
  158. *
  159. * Must be called ceremoniously while in possession of
  160. * ecryptfs_daemon_hash_mux and the daemon's own mux.
  161. */
  162. int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
  163. {
  164. struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
  165. int rc = 0;
  166. mutex_lock(&daemon->mux);
  167. if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  168. || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
  169. rc = -EBUSY;
  170. mutex_unlock(&daemon->mux);
  171. goto out;
  172. }
  173. list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
  174. &daemon->msg_ctx_out_queue, daemon_out_list) {
  175. list_del(&msg_ctx->daemon_out_list);
  176. daemon->num_queued_msg_ctx--;
  177. printk(KERN_WARNING "%s: Warning: dropping message that is in "
  178. "the out queue of a dying daemon\n", __func__);
  179. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  180. }
  181. hlist_del(&daemon->euid_chain);
  182. mutex_unlock(&daemon->mux);
  183. kzfree(daemon);
  184. out:
  185. return rc;
  186. }
  187. /**
  188. * ecryptfs_process_reponse
  189. * @msg: The ecryptfs message received; the caller should sanity check
  190. * msg->data_len and free the memory
  191. * @seq: The sequence number of the message; must match the sequence
  192. * number for the existing message context waiting for this
  193. * response
  194. *
  195. * Processes a response message after sending an operation request to
  196. * userspace. Some other process is awaiting this response. Before
  197. * sending out its first communications, the other process allocated a
  198. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  199. * response message contains this index so that we can copy over the
  200. * response message into the msg_ctx that the process holds a
  201. * reference to. The other process is going to wake up, check to see
  202. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  203. * proceed to read off and process the response message. Returns zero
  204. * upon delivery to desired context element; non-zero upon delivery
  205. * failure or error.
  206. *
  207. * Returns zero on success; non-zero otherwise
  208. */
  209. int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
  210. struct ecryptfs_message *msg, u32 seq)
  211. {
  212. struct ecryptfs_msg_ctx *msg_ctx;
  213. size_t msg_size;
  214. int rc;
  215. if (msg->index >= ecryptfs_message_buf_len) {
  216. rc = -EINVAL;
  217. printk(KERN_ERR "%s: Attempt to reference "
  218. "context buffer at index [%d]; maximum "
  219. "allowable is [%d]\n", __func__, msg->index,
  220. (ecryptfs_message_buf_len - 1));
  221. goto out;
  222. }
  223. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  224. mutex_lock(&msg_ctx->mux);
  225. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  226. rc = -EINVAL;
  227. printk(KERN_WARNING "%s: Desired context element is not "
  228. "pending a response\n", __func__);
  229. goto unlock;
  230. } else if (msg_ctx->counter != seq) {
  231. rc = -EINVAL;
  232. printk(KERN_WARNING "%s: Invalid message sequence; "
  233. "expected [%d]; received [%d]\n", __func__,
  234. msg_ctx->counter, seq);
  235. goto unlock;
  236. }
  237. msg_size = (sizeof(*msg) + msg->data_len);
  238. msg_ctx->msg = kmalloc(msg_size, GFP_KERNEL);
  239. if (!msg_ctx->msg) {
  240. rc = -ENOMEM;
  241. printk(KERN_ERR "%s: Failed to allocate [%zd] bytes of "
  242. "GFP_KERNEL memory\n", __func__, msg_size);
  243. goto unlock;
  244. }
  245. memcpy(msg_ctx->msg, msg, msg_size);
  246. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  247. wake_up_process(msg_ctx->task);
  248. rc = 0;
  249. unlock:
  250. mutex_unlock(&msg_ctx->mux);
  251. out:
  252. return rc;
  253. }
  254. /**
  255. * ecryptfs_send_message_locked
  256. * @data: The data to send
  257. * @data_len: The length of data
  258. * @msg_ctx: The message context allocated for the send
  259. *
  260. * Must be called with ecryptfs_daemon_hash_mux held.
  261. *
  262. * Returns zero on success; non-zero otherwise
  263. */
  264. static int
  265. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  266. struct ecryptfs_msg_ctx **msg_ctx)
  267. {
  268. struct ecryptfs_daemon *daemon;
  269. int rc;
  270. rc = ecryptfs_find_daemon_by_euid(&daemon);
  271. if (rc || !daemon) {
  272. rc = -ENOTCONN;
  273. goto out;
  274. }
  275. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  276. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  277. if (rc) {
  278. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  279. printk(KERN_WARNING "%s: Could not claim a free "
  280. "context element\n", __func__);
  281. goto out;
  282. }
  283. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  284. mutex_unlock(&(*msg_ctx)->mux);
  285. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  286. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  287. daemon);
  288. if (rc)
  289. printk(KERN_ERR "%s: Error attempting to send message to "
  290. "userspace daemon; rc = [%d]\n", __func__, rc);
  291. out:
  292. return rc;
  293. }
  294. /**
  295. * ecryptfs_send_message
  296. * @data: The data to send
  297. * @data_len: The length of data
  298. * @msg_ctx: The message context allocated for the send
  299. *
  300. * Grabs ecryptfs_daemon_hash_mux.
  301. *
  302. * Returns zero on success; non-zero otherwise
  303. */
  304. int ecryptfs_send_message(char *data, int data_len,
  305. struct ecryptfs_msg_ctx **msg_ctx)
  306. {
  307. int rc;
  308. mutex_lock(&ecryptfs_daemon_hash_mux);
  309. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  310. msg_ctx);
  311. mutex_unlock(&ecryptfs_daemon_hash_mux);
  312. return rc;
  313. }
  314. /**
  315. * ecryptfs_wait_for_response
  316. * @msg_ctx: The context that was assigned when sending a message
  317. * @msg: The incoming message from userspace; not set if rc != 0
  318. *
  319. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  320. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  321. * returned, msg will point to a valid message from userspace; a
  322. * non-zero value is returned upon failure to receive a message or an
  323. * error occurs. Callee must free @msg on success.
  324. */
  325. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  326. struct ecryptfs_message **msg)
  327. {
  328. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  329. int rc = 0;
  330. sleep:
  331. timeout = schedule_timeout_interruptible(timeout);
  332. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  333. mutex_lock(&msg_ctx->mux);
  334. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  335. if (timeout) {
  336. mutex_unlock(&msg_ctx->mux);
  337. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  338. goto sleep;
  339. }
  340. rc = -ENOMSG;
  341. } else {
  342. *msg = msg_ctx->msg;
  343. msg_ctx->msg = NULL;
  344. }
  345. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  346. mutex_unlock(&msg_ctx->mux);
  347. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  348. return rc;
  349. }
  350. int __init ecryptfs_init_messaging(void)
  351. {
  352. int i;
  353. int rc = 0;
  354. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  355. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  356. printk(KERN_WARNING "%s: Specified number of users is "
  357. "too large, defaulting to [%d] users\n", __func__,
  358. ecryptfs_number_of_users);
  359. }
  360. mutex_init(&ecryptfs_daemon_hash_mux);
  361. mutex_lock(&ecryptfs_daemon_hash_mux);
  362. ecryptfs_hash_bits = 1;
  363. while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
  364. ecryptfs_hash_bits++;
  365. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  366. * (1 << ecryptfs_hash_bits)),
  367. GFP_KERNEL);
  368. if (!ecryptfs_daemon_hash) {
  369. rc = -ENOMEM;
  370. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  371. mutex_unlock(&ecryptfs_daemon_hash_mux);
  372. goto out;
  373. }
  374. for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
  375. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  376. mutex_unlock(&ecryptfs_daemon_hash_mux);
  377. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  378. * ecryptfs_message_buf_len),
  379. GFP_KERNEL);
  380. if (!ecryptfs_msg_ctx_arr) {
  381. rc = -ENOMEM;
  382. printk(KERN_ERR "%s: Failed to allocate memory\n", __func__);
  383. goto out;
  384. }
  385. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  386. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  387. ecryptfs_msg_counter = 0;
  388. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  389. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  390. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  391. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  392. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  393. ecryptfs_msg_ctx_arr[i].index = i;
  394. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  395. ecryptfs_msg_ctx_arr[i].counter = 0;
  396. ecryptfs_msg_ctx_arr[i].task = NULL;
  397. ecryptfs_msg_ctx_arr[i].msg = NULL;
  398. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  399. &ecryptfs_msg_ctx_free_list);
  400. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  401. }
  402. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  403. rc = ecryptfs_init_ecryptfs_miscdev();
  404. if (rc)
  405. ecryptfs_release_messaging();
  406. out:
  407. return rc;
  408. }
  409. void ecryptfs_release_messaging(void)
  410. {
  411. if (ecryptfs_msg_ctx_arr) {
  412. int i;
  413. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  414. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  415. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  416. if (ecryptfs_msg_ctx_arr[i].msg)
  417. kfree(ecryptfs_msg_ctx_arr[i].msg);
  418. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  419. }
  420. kfree(ecryptfs_msg_ctx_arr);
  421. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  422. }
  423. if (ecryptfs_daemon_hash) {
  424. struct hlist_node *elem;
  425. struct ecryptfs_daemon *daemon;
  426. int i;
  427. mutex_lock(&ecryptfs_daemon_hash_mux);
  428. for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
  429. int rc;
  430. hlist_for_each_entry(daemon, elem,
  431. &ecryptfs_daemon_hash[i],
  432. euid_chain) {
  433. rc = ecryptfs_exorcise_daemon(daemon);
  434. if (rc)
  435. printk(KERN_ERR "%s: Error whilst "
  436. "attempting to destroy daemon; "
  437. "rc = [%d]. Dazed and confused, "
  438. "but trying to continue.\n",
  439. __func__, rc);
  440. }
  441. }
  442. kfree(ecryptfs_daemon_hash);
  443. mutex_unlock(&ecryptfs_daemon_hash_mux);
  444. }
  445. ecryptfs_destroy_ecryptfs_miscdev();
  446. return;
  447. }