miscdev.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**
  3. * eCryptfs: Linux filesystem encryption layer
  4. *
  5. * Copyright (C) 2008 International Business Machines Corp.
  6. * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/hash.h>
  10. #include <linux/random.h>
  11. #include <linux/miscdevice.h>
  12. #include <linux/poll.h>
  13. #include <linux/slab.h>
  14. #include <linux/wait.h>
  15. #include <linux/module.h>
  16. #include "ecryptfs_kernel.h"
  17. static atomic_t ecryptfs_num_miscdev_opens;
  18. /**
  19. * ecryptfs_miscdev_poll
  20. * @file: dev file
  21. * @pt: dev poll table (ignored)
  22. *
  23. * Returns the poll mask
  24. */
  25. static __poll_t
  26. ecryptfs_miscdev_poll(struct file *file, poll_table *pt)
  27. {
  28. struct ecryptfs_daemon *daemon = file->private_data;
  29. __poll_t mask = 0;
  30. mutex_lock(&daemon->mux);
  31. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  32. printk(KERN_WARNING "%s: Attempt to poll on zombified "
  33. "daemon\n", __func__);
  34. goto out_unlock_daemon;
  35. }
  36. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ)
  37. goto out_unlock_daemon;
  38. if (daemon->flags & ECRYPTFS_DAEMON_IN_POLL)
  39. goto out_unlock_daemon;
  40. daemon->flags |= ECRYPTFS_DAEMON_IN_POLL;
  41. mutex_unlock(&daemon->mux);
  42. poll_wait(file, &daemon->wait, pt);
  43. mutex_lock(&daemon->mux);
  44. if (!list_empty(&daemon->msg_ctx_out_queue))
  45. mask |= EPOLLIN | EPOLLRDNORM;
  46. out_unlock_daemon:
  47. daemon->flags &= ~ECRYPTFS_DAEMON_IN_POLL;
  48. mutex_unlock(&daemon->mux);
  49. return mask;
  50. }
  51. /**
  52. * ecryptfs_miscdev_open
  53. * @inode: inode of miscdev handle (ignored)
  54. * @file: file for miscdev handle
  55. *
  56. * Returns zero on success; non-zero otherwise
  57. */
  58. static int
  59. ecryptfs_miscdev_open(struct inode *inode, struct file *file)
  60. {
  61. struct ecryptfs_daemon *daemon = NULL;
  62. int rc;
  63. mutex_lock(&ecryptfs_daemon_hash_mux);
  64. rc = ecryptfs_find_daemon_by_euid(&daemon);
  65. if (!rc) {
  66. rc = -EINVAL;
  67. goto out_unlock_daemon_list;
  68. }
  69. rc = ecryptfs_spawn_daemon(&daemon, file);
  70. if (rc) {
  71. printk(KERN_ERR "%s: Error attempting to spawn daemon; "
  72. "rc = [%d]\n", __func__, rc);
  73. goto out_unlock_daemon_list;
  74. }
  75. mutex_lock(&daemon->mux);
  76. if (daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN) {
  77. rc = -EBUSY;
  78. goto out_unlock_daemon;
  79. }
  80. daemon->flags |= ECRYPTFS_DAEMON_MISCDEV_OPEN;
  81. file->private_data = daemon;
  82. atomic_inc(&ecryptfs_num_miscdev_opens);
  83. out_unlock_daemon:
  84. mutex_unlock(&daemon->mux);
  85. out_unlock_daemon_list:
  86. mutex_unlock(&ecryptfs_daemon_hash_mux);
  87. return rc;
  88. }
  89. /**
  90. * ecryptfs_miscdev_release
  91. * @inode: inode of fs/ecryptfs/euid handle (ignored)
  92. * @file: file for fs/ecryptfs/euid handle
  93. *
  94. * This keeps the daemon registered until the daemon sends another
  95. * ioctl to fs/ecryptfs/ctl or until the kernel module unregisters.
  96. *
  97. * Returns zero on success; non-zero otherwise
  98. */
  99. static int
  100. ecryptfs_miscdev_release(struct inode *inode, struct file *file)
  101. {
  102. struct ecryptfs_daemon *daemon = file->private_data;
  103. int rc;
  104. mutex_lock(&daemon->mux);
  105. BUG_ON(!(daemon->flags & ECRYPTFS_DAEMON_MISCDEV_OPEN));
  106. daemon->flags &= ~ECRYPTFS_DAEMON_MISCDEV_OPEN;
  107. atomic_dec(&ecryptfs_num_miscdev_opens);
  108. mutex_unlock(&daemon->mux);
  109. mutex_lock(&ecryptfs_daemon_hash_mux);
  110. rc = ecryptfs_exorcise_daemon(daemon);
  111. mutex_unlock(&ecryptfs_daemon_hash_mux);
  112. if (rc) {
  113. printk(KERN_CRIT "%s: Fatal error whilst attempting to "
  114. "shut down daemon; rc = [%d]. Please report this "
  115. "bug.\n", __func__, rc);
  116. BUG();
  117. }
  118. return rc;
  119. }
  120. /**
  121. * ecryptfs_send_miscdev
  122. * @data: Data to send to daemon; may be NULL
  123. * @data_size: Amount of data to send to daemon
  124. * @msg_ctx: Message context, which is used to handle the reply. If
  125. * this is NULL, then we do not expect a reply.
  126. * @msg_type: Type of message
  127. * @msg_flags: Flags for message
  128. * @daemon: eCryptfs daemon object
  129. *
  130. * Add msg_ctx to queue and then, if it exists, notify the blocked
  131. * miscdevess about the data being available. Must be called with
  132. * ecryptfs_daemon_hash_mux held.
  133. *
  134. * Returns zero on success; non-zero otherwise
  135. */
  136. int ecryptfs_send_miscdev(char *data, size_t data_size,
  137. struct ecryptfs_msg_ctx *msg_ctx, u8 msg_type,
  138. u16 msg_flags, struct ecryptfs_daemon *daemon)
  139. {
  140. struct ecryptfs_message *msg;
  141. msg = kmalloc((sizeof(*msg) + data_size), GFP_KERNEL);
  142. if (!msg)
  143. return -ENOMEM;
  144. mutex_lock(&msg_ctx->mux);
  145. msg_ctx->msg = msg;
  146. msg_ctx->msg->index = msg_ctx->index;
  147. msg_ctx->msg->data_len = data_size;
  148. msg_ctx->type = msg_type;
  149. memcpy(msg_ctx->msg->data, data, data_size);
  150. msg_ctx->msg_size = (sizeof(*msg_ctx->msg) + data_size);
  151. list_add_tail(&msg_ctx->daemon_out_list, &daemon->msg_ctx_out_queue);
  152. mutex_unlock(&msg_ctx->mux);
  153. mutex_lock(&daemon->mux);
  154. daemon->num_queued_msg_ctx++;
  155. wake_up_interruptible(&daemon->wait);
  156. mutex_unlock(&daemon->mux);
  157. return 0;
  158. }
  159. /*
  160. * miscdevfs packet format:
  161. * Octet 0: Type
  162. * Octets 1-4: network byte order msg_ctx->counter
  163. * Octets 5-N0: Size of struct ecryptfs_message to follow
  164. * Octets N0-N1: struct ecryptfs_message (including data)
  165. *
  166. * Octets 5-N1 not written if the packet type does not include a message
  167. */
  168. #define PKT_TYPE_SIZE 1
  169. #define PKT_CTR_SIZE 4
  170. #define MIN_NON_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  171. #define MIN_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  172. + ECRYPTFS_MIN_PKT_LEN_SIZE)
  173. /* 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES comes from tag 65 packet format */
  174. #define MAX_MSG_PKT_SIZE (PKT_TYPE_SIZE + PKT_CTR_SIZE \
  175. + ECRYPTFS_MAX_PKT_LEN_SIZE \
  176. + sizeof(struct ecryptfs_message) \
  177. + 4 + ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES)
  178. #define PKT_TYPE_OFFSET 0
  179. #define PKT_CTR_OFFSET PKT_TYPE_SIZE
  180. #define PKT_LEN_OFFSET (PKT_TYPE_SIZE + PKT_CTR_SIZE)
  181. /**
  182. * ecryptfs_miscdev_read - format and send message from queue
  183. * @file: miscdevfs handle
  184. * @buf: User buffer into which to copy the next message on the daemon queue
  185. * @count: Amount of space available in @buf
  186. * @ppos: Offset in file (ignored)
  187. *
  188. * Pulls the most recent message from the daemon queue, formats it for
  189. * being sent via a miscdevfs handle, and copies it into @buf
  190. *
  191. * Returns the number of bytes copied into the user buffer
  192. */
  193. static ssize_t
  194. ecryptfs_miscdev_read(struct file *file, char __user *buf, size_t count,
  195. loff_t *ppos)
  196. {
  197. struct ecryptfs_daemon *daemon = file->private_data;
  198. struct ecryptfs_msg_ctx *msg_ctx;
  199. size_t packet_length_size;
  200. char packet_length[ECRYPTFS_MAX_PKT_LEN_SIZE];
  201. size_t i;
  202. size_t total_length;
  203. int rc;
  204. mutex_lock(&daemon->mux);
  205. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  206. rc = 0;
  207. printk(KERN_WARNING "%s: Attempt to read from zombified "
  208. "daemon\n", __func__);
  209. goto out_unlock_daemon;
  210. }
  211. if (daemon->flags & ECRYPTFS_DAEMON_IN_READ) {
  212. rc = 0;
  213. goto out_unlock_daemon;
  214. }
  215. /* This daemon will not go away so long as this flag is set */
  216. daemon->flags |= ECRYPTFS_DAEMON_IN_READ;
  217. check_list:
  218. if (list_empty(&daemon->msg_ctx_out_queue)) {
  219. mutex_unlock(&daemon->mux);
  220. rc = wait_event_interruptible(
  221. daemon->wait, !list_empty(&daemon->msg_ctx_out_queue));
  222. mutex_lock(&daemon->mux);
  223. if (rc < 0) {
  224. rc = 0;
  225. goto out_unlock_daemon;
  226. }
  227. }
  228. if (daemon->flags & ECRYPTFS_DAEMON_ZOMBIE) {
  229. rc = 0;
  230. goto out_unlock_daemon;
  231. }
  232. if (list_empty(&daemon->msg_ctx_out_queue)) {
  233. /* Something else jumped in since the
  234. * wait_event_interruptable() and removed the
  235. * message from the queue; try again */
  236. goto check_list;
  237. }
  238. msg_ctx = list_first_entry(&daemon->msg_ctx_out_queue,
  239. struct ecryptfs_msg_ctx, daemon_out_list);
  240. BUG_ON(!msg_ctx);
  241. mutex_lock(&msg_ctx->mux);
  242. if (msg_ctx->msg) {
  243. rc = ecryptfs_write_packet_length(packet_length,
  244. msg_ctx->msg_size,
  245. &packet_length_size);
  246. if (rc) {
  247. rc = 0;
  248. printk(KERN_WARNING "%s: Error writing packet length; "
  249. "rc = [%d]\n", __func__, rc);
  250. goto out_unlock_msg_ctx;
  251. }
  252. } else {
  253. packet_length_size = 0;
  254. msg_ctx->msg_size = 0;
  255. }
  256. total_length = (PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_length_size
  257. + msg_ctx->msg_size);
  258. if (count < total_length) {
  259. rc = 0;
  260. printk(KERN_WARNING "%s: Only given user buffer of "
  261. "size [%zd], but we need [%zd] to read the "
  262. "pending message\n", __func__, count, total_length);
  263. goto out_unlock_msg_ctx;
  264. }
  265. rc = -EFAULT;
  266. if (put_user(msg_ctx->type, buf))
  267. goto out_unlock_msg_ctx;
  268. if (put_user(cpu_to_be32(msg_ctx->counter),
  269. (__be32 __user *)(&buf[PKT_CTR_OFFSET])))
  270. goto out_unlock_msg_ctx;
  271. i = PKT_TYPE_SIZE + PKT_CTR_SIZE;
  272. if (msg_ctx->msg) {
  273. if (copy_to_user(&buf[i], packet_length, packet_length_size))
  274. goto out_unlock_msg_ctx;
  275. i += packet_length_size;
  276. if (copy_to_user(&buf[i], msg_ctx->msg, msg_ctx->msg_size))
  277. goto out_unlock_msg_ctx;
  278. i += msg_ctx->msg_size;
  279. }
  280. rc = i;
  281. list_del(&msg_ctx->daemon_out_list);
  282. kfree(msg_ctx->msg);
  283. msg_ctx->msg = NULL;
  284. /* We do not expect a reply from the userspace daemon for any
  285. * message type other than ECRYPTFS_MSG_REQUEST */
  286. if (msg_ctx->type != ECRYPTFS_MSG_REQUEST)
  287. ecryptfs_msg_ctx_alloc_to_free(msg_ctx);
  288. out_unlock_msg_ctx:
  289. mutex_unlock(&msg_ctx->mux);
  290. out_unlock_daemon:
  291. daemon->flags &= ~ECRYPTFS_DAEMON_IN_READ;
  292. mutex_unlock(&daemon->mux);
  293. return rc;
  294. }
  295. /**
  296. * ecryptfs_miscdev_response - miscdevess response to message previously sent to daemon
  297. * @data: Bytes comprising struct ecryptfs_message
  298. * @data_size: sizeof(struct ecryptfs_message) + data len
  299. * @seq: Sequence number for miscdev response packet
  300. *
  301. * Returns zero on success; non-zero otherwise
  302. */
  303. static int ecryptfs_miscdev_response(struct ecryptfs_daemon *daemon, char *data,
  304. size_t data_size, u32 seq)
  305. {
  306. struct ecryptfs_message *msg = (struct ecryptfs_message *)data;
  307. int rc;
  308. if ((sizeof(*msg) + msg->data_len) != data_size) {
  309. printk(KERN_WARNING "%s: (sizeof(*msg) + msg->data_len) = "
  310. "[%zd]; data_size = [%zd]. Invalid packet.\n", __func__,
  311. (sizeof(*msg) + msg->data_len), data_size);
  312. rc = -EINVAL;
  313. goto out;
  314. }
  315. rc = ecryptfs_process_response(daemon, msg, seq);
  316. if (rc)
  317. printk(KERN_ERR
  318. "Error processing response message; rc = [%d]\n", rc);
  319. out:
  320. return rc;
  321. }
  322. /**
  323. * ecryptfs_miscdev_write - handle write to daemon miscdev handle
  324. * @file: File for misc dev handle
  325. * @buf: Buffer containing user data
  326. * @count: Amount of data in @buf
  327. * @ppos: Pointer to offset in file (ignored)
  328. *
  329. * Returns the number of bytes read from @buf
  330. */
  331. static ssize_t
  332. ecryptfs_miscdev_write(struct file *file, const char __user *buf,
  333. size_t count, loff_t *ppos)
  334. {
  335. __be32 counter_nbo;
  336. u32 seq;
  337. size_t packet_size, packet_size_length;
  338. char *data;
  339. unsigned char packet_size_peek[ECRYPTFS_MAX_PKT_LEN_SIZE];
  340. ssize_t rc;
  341. if (count == 0) {
  342. return 0;
  343. } else if (count == MIN_NON_MSG_PKT_SIZE) {
  344. /* Likely a harmless MSG_HELO or MSG_QUIT - no packet length */
  345. goto memdup;
  346. } else if (count < MIN_MSG_PKT_SIZE || count > MAX_MSG_PKT_SIZE) {
  347. printk(KERN_WARNING "%s: Acceptable packet size range is "
  348. "[%d-%zu], but amount of data written is [%zu].\n",
  349. __func__, MIN_MSG_PKT_SIZE, MAX_MSG_PKT_SIZE, count);
  350. return -EINVAL;
  351. }
  352. if (copy_from_user(packet_size_peek, &buf[PKT_LEN_OFFSET],
  353. sizeof(packet_size_peek))) {
  354. printk(KERN_WARNING "%s: Error while inspecting packet size\n",
  355. __func__);
  356. return -EFAULT;
  357. }
  358. rc = ecryptfs_parse_packet_length(packet_size_peek, &packet_size,
  359. &packet_size_length);
  360. if (rc) {
  361. printk(KERN_WARNING "%s: Error parsing packet length; "
  362. "rc = [%zd]\n", __func__, rc);
  363. return rc;
  364. }
  365. if ((PKT_TYPE_SIZE + PKT_CTR_SIZE + packet_size_length + packet_size)
  366. != count) {
  367. printk(KERN_WARNING "%s: Invalid packet size [%zu]\n", __func__,
  368. packet_size);
  369. return -EINVAL;
  370. }
  371. memdup:
  372. data = memdup_user(buf, count);
  373. if (IS_ERR(data)) {
  374. printk(KERN_ERR "%s: memdup_user returned error [%ld]\n",
  375. __func__, PTR_ERR(data));
  376. return PTR_ERR(data);
  377. }
  378. switch (data[PKT_TYPE_OFFSET]) {
  379. case ECRYPTFS_MSG_RESPONSE:
  380. if (count < (MIN_MSG_PKT_SIZE
  381. + sizeof(struct ecryptfs_message))) {
  382. printk(KERN_WARNING "%s: Minimum acceptable packet "
  383. "size is [%zd], but amount of data written is "
  384. "only [%zd]. Discarding response packet.\n",
  385. __func__,
  386. (MIN_MSG_PKT_SIZE
  387. + sizeof(struct ecryptfs_message)), count);
  388. rc = -EINVAL;
  389. goto out_free;
  390. }
  391. memcpy(&counter_nbo, &data[PKT_CTR_OFFSET], PKT_CTR_SIZE);
  392. seq = be32_to_cpu(counter_nbo);
  393. rc = ecryptfs_miscdev_response(file->private_data,
  394. &data[PKT_LEN_OFFSET + packet_size_length],
  395. packet_size, seq);
  396. if (rc) {
  397. printk(KERN_WARNING "%s: Failed to deliver miscdev "
  398. "response to requesting operation; rc = [%zd]\n",
  399. __func__, rc);
  400. goto out_free;
  401. }
  402. break;
  403. case ECRYPTFS_MSG_HELO:
  404. case ECRYPTFS_MSG_QUIT:
  405. break;
  406. default:
  407. ecryptfs_printk(KERN_WARNING, "Dropping miscdev "
  408. "message of unrecognized type [%d]\n",
  409. data[0]);
  410. rc = -EINVAL;
  411. goto out_free;
  412. }
  413. rc = count;
  414. out_free:
  415. kfree(data);
  416. return rc;
  417. }
  418. static const struct file_operations ecryptfs_miscdev_fops = {
  419. .owner = THIS_MODULE,
  420. .open = ecryptfs_miscdev_open,
  421. .poll = ecryptfs_miscdev_poll,
  422. .read = ecryptfs_miscdev_read,
  423. .write = ecryptfs_miscdev_write,
  424. .release = ecryptfs_miscdev_release,
  425. .llseek = noop_llseek,
  426. };
  427. static struct miscdevice ecryptfs_miscdev = {
  428. .minor = MISC_DYNAMIC_MINOR,
  429. .name = "ecryptfs",
  430. .fops = &ecryptfs_miscdev_fops
  431. };
  432. /**
  433. * ecryptfs_init_ecryptfs_miscdev
  434. *
  435. * Messages sent to the userspace daemon from the kernel are placed on
  436. * a queue associated with the daemon. The next read against the
  437. * miscdev handle by that daemon will return the oldest message placed
  438. * on the message queue for the daemon.
  439. *
  440. * Returns zero on success; non-zero otherwise
  441. */
  442. int __init ecryptfs_init_ecryptfs_miscdev(void)
  443. {
  444. int rc;
  445. atomic_set(&ecryptfs_num_miscdev_opens, 0);
  446. rc = misc_register(&ecryptfs_miscdev);
  447. if (rc)
  448. printk(KERN_ERR "%s: Failed to register miscellaneous device "
  449. "for communications with userspace daemons; rc = [%d]\n",
  450. __func__, rc);
  451. return rc;
  452. }
  453. /**
  454. * ecryptfs_destroy_ecryptfs_miscdev
  455. *
  456. * All of the daemons must be exorcised prior to calling this
  457. * function.
  458. */
  459. void ecryptfs_destroy_ecryptfs_miscdev(void)
  460. {
  461. BUG_ON(atomic_read(&ecryptfs_num_miscdev_opens) != 0);
  462. misc_deregister(&ecryptfs_miscdev);
  463. }