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 <mhalcrow@us.ibm.com>
  7. * Tyler Hicks <code@tyhicks.com>
  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 struct mutex ecryptfs_msg_ctx_lists_mux;
  17. static struct hlist_head *ecryptfs_daemon_hash;
  18. struct 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_reponse
  171. * @msg: The ecryptfs message received; the caller should sanity check
  172. * msg->data_len and free the memory
  173. * @seq: The sequence number of the message; must match the sequence
  174. * number for the existing message context waiting for this
  175. * response
  176. *
  177. * Processes a response message after sending an operation request to
  178. * userspace. Some other process is awaiting this response. Before
  179. * sending out its first communications, the other process allocated a
  180. * msg_ctx from the ecryptfs_msg_ctx_arr at a particular index. The
  181. * response message contains this index so that we can copy over the
  182. * response message into the msg_ctx that the process holds a
  183. * reference to. The other process is going to wake up, check to see
  184. * that msg_ctx->state == ECRYPTFS_MSG_CTX_STATE_DONE, and then
  185. * proceed to read off and process the response message. Returns zero
  186. * upon delivery to desired context element; non-zero upon delivery
  187. * failure or error.
  188. *
  189. * Returns zero on success; non-zero otherwise
  190. */
  191. int ecryptfs_process_response(struct ecryptfs_daemon *daemon,
  192. struct ecryptfs_message *msg, u32 seq)
  193. {
  194. struct ecryptfs_msg_ctx *msg_ctx;
  195. size_t msg_size;
  196. int rc;
  197. if (msg->index >= ecryptfs_message_buf_len) {
  198. rc = -EINVAL;
  199. printk(KERN_ERR "%s: Attempt to reference "
  200. "context buffer at index [%d]; maximum "
  201. "allowable is [%d]\n", __func__, msg->index,
  202. (ecryptfs_message_buf_len - 1));
  203. goto out;
  204. }
  205. msg_ctx = &ecryptfs_msg_ctx_arr[msg->index];
  206. mutex_lock(&msg_ctx->mux);
  207. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_PENDING) {
  208. rc = -EINVAL;
  209. printk(KERN_WARNING "%s: Desired context element is not "
  210. "pending a response\n", __func__);
  211. goto unlock;
  212. } else if (msg_ctx->counter != seq) {
  213. rc = -EINVAL;
  214. printk(KERN_WARNING "%s: Invalid message sequence; "
  215. "expected [%d]; received [%d]\n", __func__,
  216. msg_ctx->counter, seq);
  217. goto unlock;
  218. }
  219. msg_size = (sizeof(*msg) + msg->data_len);
  220. msg_ctx->msg = kmemdup(msg, msg_size, GFP_KERNEL);
  221. if (!msg_ctx->msg) {
  222. rc = -ENOMEM;
  223. goto unlock;
  224. }
  225. msg_ctx->state = ECRYPTFS_MSG_CTX_STATE_DONE;
  226. wake_up_process(msg_ctx->task);
  227. rc = 0;
  228. unlock:
  229. mutex_unlock(&msg_ctx->mux);
  230. out:
  231. return rc;
  232. }
  233. /**
  234. * ecryptfs_send_message_locked
  235. * @data: The data to send
  236. * @data_len: The length of data
  237. * @msg_ctx: The message context allocated for the send
  238. *
  239. * Must be called with ecryptfs_daemon_hash_mux held.
  240. *
  241. * Returns zero on success; non-zero otherwise
  242. */
  243. static int
  244. ecryptfs_send_message_locked(char *data, int data_len, u8 msg_type,
  245. struct ecryptfs_msg_ctx **msg_ctx)
  246. {
  247. struct ecryptfs_daemon *daemon;
  248. int rc;
  249. rc = ecryptfs_find_daemon_by_euid(&daemon);
  250. if (rc) {
  251. rc = -ENOTCONN;
  252. goto out;
  253. }
  254. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  255. rc = ecryptfs_acquire_free_msg_ctx(msg_ctx);
  256. if (rc) {
  257. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  258. printk(KERN_WARNING "%s: Could not claim a free "
  259. "context element\n", __func__);
  260. goto out;
  261. }
  262. ecryptfs_msg_ctx_free_to_alloc(*msg_ctx);
  263. mutex_unlock(&(*msg_ctx)->mux);
  264. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  265. rc = ecryptfs_send_miscdev(data, data_len, *msg_ctx, msg_type, 0,
  266. daemon);
  267. if (rc)
  268. printk(KERN_ERR "%s: Error attempting to send message to "
  269. "userspace daemon; rc = [%d]\n", __func__, rc);
  270. out:
  271. return rc;
  272. }
  273. /**
  274. * ecryptfs_send_message
  275. * @data: The data to send
  276. * @data_len: The length of data
  277. * @msg_ctx: The message context allocated for the send
  278. *
  279. * Grabs ecryptfs_daemon_hash_mux.
  280. *
  281. * Returns zero on success; non-zero otherwise
  282. */
  283. int ecryptfs_send_message(char *data, int data_len,
  284. struct ecryptfs_msg_ctx **msg_ctx)
  285. {
  286. int rc;
  287. mutex_lock(&ecryptfs_daemon_hash_mux);
  288. rc = ecryptfs_send_message_locked(data, data_len, ECRYPTFS_MSG_REQUEST,
  289. msg_ctx);
  290. mutex_unlock(&ecryptfs_daemon_hash_mux);
  291. return rc;
  292. }
  293. /**
  294. * ecryptfs_wait_for_response
  295. * @msg_ctx: The context that was assigned when sending a message
  296. * @msg: The incoming message from userspace; not set if rc != 0
  297. *
  298. * Sleeps until awaken by ecryptfs_receive_message or until the amount
  299. * of time exceeds ecryptfs_message_wait_timeout. If zero is
  300. * returned, msg will point to a valid message from userspace; a
  301. * non-zero value is returned upon failure to receive a message or an
  302. * error occurs. Callee must free @msg on success.
  303. */
  304. int ecryptfs_wait_for_response(struct ecryptfs_msg_ctx *msg_ctx,
  305. struct ecryptfs_message **msg)
  306. {
  307. signed long timeout = ecryptfs_message_wait_timeout * HZ;
  308. int rc = 0;
  309. sleep:
  310. timeout = schedule_timeout_interruptible(timeout);
  311. mutex_lock(&ecryptfs_msg_ctx_lists_mux);
  312. mutex_lock(&msg_ctx->mux);
  313. if (msg_ctx->state != ECRYPTFS_MSG_CTX_STATE_DONE) {
  314. if (timeout) {
  315. mutex_unlock(&msg_ctx->mux);
  316. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  317. goto sleep;
  318. }
  319. rc = -ENOMSG;
  320. } else {
  321. *msg = msg_ctx->msg;
  322. msg_ctx->msg = NULL;
  323. }
  324. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  325. mutex_unlock(&msg_ctx->mux);
  326. mutex_unlock(&ecryptfs_msg_ctx_lists_mux);
  327. return rc;
  328. }
  329. int __init ecryptfs_init_messaging(void)
  330. {
  331. int i;
  332. int rc = 0;
  333. if (ecryptfs_number_of_users > ECRYPTFS_MAX_NUM_USERS) {
  334. ecryptfs_number_of_users = ECRYPTFS_MAX_NUM_USERS;
  335. printk(KERN_WARNING "%s: Specified number of users is "
  336. "too large, defaulting to [%d] users\n", __func__,
  337. ecryptfs_number_of_users);
  338. }
  339. mutex_init(&ecryptfs_daemon_hash_mux);
  340. mutex_lock(&ecryptfs_daemon_hash_mux);
  341. ecryptfs_hash_bits = 1;
  342. while (ecryptfs_number_of_users >> ecryptfs_hash_bits)
  343. ecryptfs_hash_bits++;
  344. ecryptfs_daemon_hash = kmalloc((sizeof(struct hlist_head)
  345. * (1 << ecryptfs_hash_bits)),
  346. GFP_KERNEL);
  347. if (!ecryptfs_daemon_hash) {
  348. rc = -ENOMEM;
  349. mutex_unlock(&ecryptfs_daemon_hash_mux);
  350. goto out;
  351. }
  352. for (i = 0; i < (1 << ecryptfs_hash_bits); i++)
  353. INIT_HLIST_HEAD(&ecryptfs_daemon_hash[i]);
  354. mutex_unlock(&ecryptfs_daemon_hash_mux);
  355. ecryptfs_msg_ctx_arr = kmalloc((sizeof(struct ecryptfs_msg_ctx)
  356. * ecryptfs_message_buf_len),
  357. GFP_KERNEL);
  358. if (!ecryptfs_msg_ctx_arr) {
  359. kfree(ecryptfs_daemon_hash);
  360. rc = -ENOMEM;
  361. goto out;
  362. }
  363. mutex_init(&ecryptfs_msg_ctx_lists_mux);
  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. }