messaging.c 14 KB

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