miscdev.c 16 KB

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