messaging.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * eCryptfs: Linux filesystem encryption layer
  4. *
  5. * Copyright (C) 2004-2008 International Business Machines Corp.
  6. * Author(s): Michael A. Halcrow <[email protected]>
  7. * Tyler Hicks <[email protected]>
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/user_namespace.h>
  12. #include <linux/nsproxy.h>
  13. #include "ecryptfs_kernel.h"
  14. static LIST_HEAD(ecryptfs_msg_ctx_free_list);
  15. static LIST_HEAD(ecryptfs_msg_ctx_alloc_list);
  16. static DEFINE_MUTEX(ecryptfs_msg_ctx_lists_mux);
  17. static struct hlist_head *ecryptfs_daemon_hash;
  18. DEFINE_MUTEX(ecryptfs_daemon_hash_mux);
  19. static int ecryptfs_hash_bits;
  20. #define ecryptfs_current_euid_hash(uid) \
  21. hash_long((unsigned long)from_kuid(&init_user_ns, current_euid()), ecryptfs_hash_bits)
  22. static u32 ecryptfs_msg_counter;
  23. static struct ecryptfs_msg_ctx *ecryptfs_msg_ctx_arr;
  24. /**
  25. * ecryptfs_acquire_free_msg_ctx
  26. * @msg_ctx: The context that was acquired from the free list
  27. *
  28. * Acquires a context element from the free list and locks the mutex
  29. * on the context. Sets the msg_ctx task to current. Returns zero on
  30. * success; non-zero on error or upon failure to acquire a free
  31. * context element. Must be called with ecryptfs_msg_ctx_lists_mux
  32. * held.
  33. */
  34. static int ecryptfs_acquire_free_msg_ctx(struct ecryptfs_msg_ctx **msg_ctx)
  35. {
  36. struct list_head *p;
  37. int rc;
  38. if (list_empty(&ecryptfs_msg_ctx_free_list)) {
  39. printk(KERN_WARNING "%s: The eCryptfs free "
  40. "context list is empty. It may be helpful to "
  41. "specify the ecryptfs_message_buf_len "
  42. "parameter to be greater than the current "
  43. "value of [%d]\n", __func__, ecryptfs_message_buf_len);
  44. rc = -ENOMEM;
  45. goto out;
  46. }
  47. list_for_each(p, &ecryptfs_msg_ctx_free_list) {
  48. *msg_ctx = list_entry(p, struct ecryptfs_msg_ctx, node);
  49. if (mutex_trylock(&(*msg_ctx)->mux)) {
  50. (*msg_ctx)->task = current;
  51. rc = 0;
  52. goto out;
  53. }
  54. }
  55. rc = -ENOMEM;
  56. out:
  57. return rc;
  58. }
  59. /**
  60. * ecryptfs_msg_ctx_free_to_alloc
  61. * @msg_ctx: The context to move from the free list to the alloc list
  62. *
  63. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  64. */
  65. static void ecryptfs_msg_ctx_free_to_alloc(struct ecryptfs_msg_ctx *msg_ctx)
  66. {
  67. list_move(&msg_ctx->node, &ecryptfs_msg_ctx_alloc_list);
  68. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_PENDING;
  69. msg_ctx->counter = ++ecryptfs_msg_counter;
  70. }
  71. /**
  72. * ecryptfs_msg_ctx_alloc_to_free
  73. * @msg_ctx: The context to move from the alloc list to the free list
  74. *
  75. * Must be called with ecryptfs_msg_ctx_lists_mux held.
  76. */
  77. void ecryptfs_msg_ctx_alloc_to_free(struct ecryptfs_msg_ctx *msg_ctx)
  78. {
  79. list_move(&(msg_ctx->node), &ecryptfs_msg_ctx_free_list);
  80. kfree(msg_ctx->msg);
  81. msg_ctx->msg = NULL;
  82. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_FREE;
  83. }
  84. /**
  85. * ecryptfs_find_daemon_by_euid
  86. * @daemon: If return value is zero, points to the desired daemon pointer
  87. *
  88. * Must be called with ecryptfs_daemon_hash_mux held.
  89. *
  90. * Search the hash list for the current effective user id.
  91. *
  92. * Returns zero if the user id exists in the list; non-zero otherwise.
  93. */
  94. int ecryptfs_find_daemon_by_euid(struct ecryptfs_daemon **daemon)
  95. {
  96. int rc;
  97. hlist_for_each_entry(*daemon,
  98. &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()],
  99. euid_chain) {
  100. if (uid_eq((*daemon)->file->f_cred->euid, current_euid())) {
  101. rc = 0;
  102. goto out;
  103. }
  104. }
  105. rc = -EINVAL;
  106. out:
  107. return rc;
  108. }
  109. /**
  110. * ecryptfs_spawn_daemon - Create and initialize a new daemon struct
  111. * @daemon: Pointer to set to newly allocated daemon struct
  112. * @file: File used when opening /dev/ecryptfs
  113. *
  114. * Must be called ceremoniously while in possession of
  115. * ecryptfs_sacred_daemon_hash_mux
  116. *
  117. * Returns zero on success; non-zero otherwise
  118. */
  119. int
  120. ecryptfs_spawn_daemon(struct ecryptfs_daemon **daemon, struct file *file)
  121. {
  122. int rc = 0;
  123. (*daemon) = kzalloc(sizeof(**daemon), GFP_KERNEL);
  124. if (!(*daemon)) {
  125. rc = -ENOMEM;
  126. goto out;
  127. }
  128. (*daemon)->file = file;
  129. mutex_init(&(*daemon)->mux);
  130. INIT_LIST_HEAD(&(*daemon)->msg_ctx_out_queue);
  131. init_waitqueue_head(&(*daemon)->wait);
  132. (*daemon)->num_queued_msg_ctx = 0;
  133. hlist_add_head(&(*daemon)->euid_chain,
  134. &ecryptfs_daemon_hash[ecryptfs_current_euid_hash()]);
  135. out:
  136. return rc;
  137. }
  138. /*
  139. * ecryptfs_exorcise_daemon - Destroy the daemon struct
  140. *
  141. * Must be called ceremoniously while in possession of
  142. * ecryptfs_daemon_hash_mux and the daemon's own mux.
  143. */
  144. int ecryptfs_exorcise_daemon(struct ecryptfs_daemon *daemon)
  145. {
  146. struct ecryptfs_msg_ctx *msg_ctx, *msg_ctx_tmp;
  147. int rc = 0;
  148. mutex_lock(&daemon->mux);
  149. if ((daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  150. || (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)) {
  151. rc = -EBUSY;
  152. mutex_unlock(&daemon->mux);
  153. goto out;
  154. }
  155. list_for_each_entry_safe(msg_ctx, msg_ctx_tmp,
  156. &daemon->msg_ctx_out_queue, daemon_out_list) {
  157. list_del(&msg_ctx->daemon_out_list);
  158. daemon->num_queued_msg_ctx--;
  159. printk(KERN_WARNING "%s: Warning: dropping message that is in "
  160. "the out queue of a dying daemon\n", __func__);
  161. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  162. }
  163. hlist_del(&daemon->euid_chain);
  164. mutex_unlock(&daemon->mux);
  165. kfree_sensitive(daemon);
  166. out:
  167. return rc;
  168. }
  169. /**
  170. * ecryptfs_process_response
  171. * @daemon: eCryptfs daemon object
  172. * @msg: The ecryptfs message received; the caller should sanity check
  173. * msg->data_len and free the memory
  174. * @seq: The sequence number of the message; must match the sequence
  175. * number for the existing message context waiting for this
  176. * response
  177. *
  178. * Processes a response message after sending an operation request to
  179. * userspace. Some other process is awaiting this response. Before
  180. * sending out its first communications, the other process allocated a
  181. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  182. * response message contains this index so that we can copy over the
  183. * response message into the msg_ctx that the process holds a
  184. * reference to. The other process is going to wake up, check to see
  185. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  186. * proceed to read off and process the response message. Returns zero
  187. * upon delivery to desired context element; non-zero upon delivery
  188. * failure or error.
  189. *
  190. * Returns zero on success; non-zero otherwise
  191. */
  192. int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
  193. struct ecryptfs_message *msg, u32 seq)
  194. {
  195. struct ecryptfs_msg_ctx *msg_ctx;
  196. size_t msg_size;
  197. int rc;
  198. if (msg->index >= ecryptfs_message_buf_len) {
  199. rc = -EINVAL;
  200. printk(KERN_ERR "%s: Attempt to reference "
  201. "context buffer at index [%d]; maximum "
  202. "allowable is [%d]\n", __func__, msg->index,
  203. (ecryptfs_message_buf_len - 1));
  204. goto out;
  205. }
  206. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  207. mutex_lock(&msg_ctx->mux);
  208. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  209. rc = -EINVAL;
  210. printk(KERN_WARNING "%s: Desired context element is not "
  211. "pending a response\n", __func__);
  212. goto unlock;
  213. } else if (msg_ctx->counter != seq) {
  214. rc = -EINVAL;
  215. printk(KERN_WARNING "%s: Invalid message sequence; "
  216. "expected [%d]; received [%d]\n", __func__,
  217. msg_ctx->counter, seq);
  218. goto unlock;
  219. }
  220. msg_size = (sizeof(*msg) + msg->data_len);
  221. msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
  222. if (!msg_ctx->msg) {
  223. rc = -ENOMEM;
  224. goto unlock;
  225. }
  226. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  227. wake_up_process(msg_ctx->task);
  228. rc = 0;
  229. unlock:
  230. mutex_unlock(&msg_ctx->mux);
  231. out:
  232. return rc;
  233. }
  234. /**
  235. * ecryptfs_send_message_locked
  236. * @data: The data to send
  237. * @data_len: The length of data
  238. * @msg_type: Type of message
  239. * @msg_ctx: The message context allocated for the send
  240. *
  241. * Must be called with ecryptfs_daemon_hash_mux held.
  242. *
  243. * Returns zero on success; non-zero otherwise
  244. */
  245. static int
  246. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  247. struct ecryptfs_msg_ctx **msg_ctx)
  248. {
  249. struct ecryptfs_daemon *daemon;
  250. int rc;
  251. rc = ecryptfs_find_daemon_by_euid(&daemon);
  252. if (rc) {
  253. rc = -ENOTCONN;
  254. goto out;
  255. }
  256. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  257. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  258. if (rc) {
  259. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  260. printk(KERN_WARNING "%s: Could not claim a free "
  261. "context element\n", __func__);
  262. goto out;
  263. }
  264. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  265. mutex_unlock(&(*msg_ctx)->mux);
  266. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  267. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  268. daemon);
  269. if (rc)
  270. printk(KERN_ERR "%s: Error attempting to send message to "
  271. "userspace daemon; rc = [%d]\n", __func__, rc);
  272. out:
  273. return rc;
  274. }
  275. /**
  276. * ecryptfs_send_message
  277. * @data: The data to send
  278. * @data_len: The length of data
  279. * @msg_ctx: The message context allocated for the send
  280. *
  281. * Grabs ecryptfs_daemon_hash_mux.
  282. *
  283. * Returns zero on success; non-zero otherwise
  284. */
  285. int ecryptfs_send_message(char *data, int data_len,
  286. struct ecryptfs_msg_ctx **msg_ctx)
  287. {
  288. int rc;
  289. mutex_lock(&ecryptfs_daemon_hash_mux);
  290. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  291. msg_ctx);
  292. mutex_unlock(&ecryptfs_daemon_hash_mux);
  293. return rc;
  294. }
  295. /**
  296. * ecryptfs_wait_for_response
  297. * @msg_ctx: The context that was assigned when sending a message
  298. * @msg: The incoming message from userspace; not set if rc != 0
  299. *
  300. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  301. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  302. * returned, msg will point to a valid message from userspace; a
  303. * non-zero value is returned upon failure to receive a message or an
  304. * error occurs. Callee must free @msg on success.
  305. */
  306. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  307. struct ecryptfs_message **msg)
  308. {
  309. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  310. int rc = 0;
  311. sleep:
  312. timeout = schedule_timeout_interruptible(timeout);
  313. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  314. mutex_lock(&msg_ctx->mux);
  315. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  316. if (timeout) {
  317. mutex_unlock(&msg_ctx->mux);
  318. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  319. goto sleep;
  320. }
  321. rc = -ENOMSG;
  322. } else {
  323. *msg = msg_ctx->msg;
  324. msg_ctx->msg = NULL;
  325. }
  326. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  327. mutex_unlock(&msg_ctx->mux);
  328. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  329. return rc;
  330. }
  331. int __init ecryptfs_init_messaging(void)
  332. {
  333. int i;
  334. int rc = 0;
  335. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  336. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  337. printk(KERN_WARNING "%s: Specified number of users is "
  338. "too large, defaulting to [%d] users\n", __func__,
  339. ecryptfs_number_of_users);
  340. }
  341. mutex_lock(&ecryptfs_daemon_hash_mux);
  342. ecryptfs_hash_bits = 1;
  343. while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
  344. ecryptfs_hash_bits++;
  345. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  346. * (1 << ecryptfs_hash_bits)),
  347. GFP_KERNEL);
  348. if (!ecryptfs_daemon_hash) {
  349. rc = -ENOMEM;
  350. mutex_unlock(&ecryptfs_daemon_hash_mux);
  351. goto out;
  352. }
  353. for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
  354. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  355. mutex_unlock(&ecryptfs_daemon_hash_mux);
  356. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  357. * ecryptfs_message_buf_len),
  358. GFP_KERNEL);
  359. if (!ecryptfs_msg_ctx_arr) {
  360. kfree(ecryptfs_daemon_hash);
  361. rc = -ENOMEM;
  362. goto out;
  363. }
  364. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  365. ecryptfs_msg_counter = 0;
  366. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  367. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].node);
  368. INIT_LIST_HEAD(&ecryptfs_msg_ctx_arr[i].daemon_out_list);
  369. mutex_init(&ecryptfs_msg_ctx_arr[i].mux);
  370. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  371. ecryptfs_msg_ctx_arr[i].index = i;
  372. ecryptfs_msg_ctx_arr[i].state = ECRYPTFS_MSG_CTX_STATE_FREE;
  373. ecryptfs_msg_ctx_arr[i].counter = 0;
  374. ecryptfs_msg_ctx_arr[i].task = NULL;
  375. ecryptfs_msg_ctx_arr[i].msg = NULL;
  376. list_add_tail(&ecryptfs_msg_ctx_arr[i].node,
  377. &ecryptfs_msg_ctx_free_list);
  378. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  379. }
  380. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  381. rc = ecryptfs_init_ecryptfs_miscdev();
  382. if (rc)
  383. ecryptfs_release_messaging();
  384. out:
  385. return rc;
  386. }
  387. void ecryptfs_release_messaging(void)
  388. {
  389. if (ecryptfs_msg_ctx_arr) {
  390. int i;
  391. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  392. for (i = 0; i < ecryptfs_message_buf_len; i++) {
  393. mutex_lock(&ecryptfs_msg_ctx_arr[i].mux);
  394. kfree(ecryptfs_msg_ctx_arr[i].msg);
  395. mutex_unlock(&ecryptfs_msg_ctx_arr[i].mux);
  396. }
  397. kfree(ecryptfs_msg_ctx_arr);
  398. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  399. }
  400. if (ecryptfs_daemon_hash) {
  401. struct ecryptfs_daemon *daemon;
  402. struct hlist_node *n;
  403. int i;
  404. mutex_lock(&ecryptfs_daemon_hash_mux);
  405. for (i = 0; i < (1 << ecryptfs_hash_bits); i++) {
  406. int rc;
  407. hlist_for_each_entry_safe(daemon, n,
  408. &ecryptfs_daemon_hash[i],
  409. euid_chain) {
  410. rc = ecryptfs_exorcise_daemon(daemon);
  411. if (rc)
  412. printk(KERN_ERR "%s: Error whilst "
  413. "attempting to destroy daemon; "
  414. "rc = [%d]. Dazed and confused, "
  415. "but trying to continue.\n",
  416. __func__, rc);
  417. }
  418. }
  419. kfree(ecryptfs_daemon_hash);
  420. mutex_unlock(&ecryptfs_daemon_hash_mux);
  421. }
  422. ecryptfs_destroy_ecryptfs_miscdev();
  423. return;
  424. }