miscdev.c 15 KB

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