|
24 | 24 | #include <linux/slab.h> |
25 | 25 | #include <linux/mutex.h> |
26 | 26 | #include <linux/poll.h> |
| 27 | +#include <linux/mount.h> |
| 28 | +#include <linux/file.h> |
| 29 | +#include <linux/ioctl.h> |
27 | 30 |
|
28 | 31 | #undef TTY_DEBUG_HANGUP |
29 | 32 | #ifdef TTY_DEBUG_HANGUP |
@@ -66,8 +69,13 @@ static void pty_close(struct tty_struct *tty, struct file *filp) |
66 | 69 | #ifdef CONFIG_UNIX98_PTYS |
67 | 70 | if (tty->driver == ptm_driver) { |
68 | 71 | mutex_lock(&devpts_mutex); |
69 | | - if (tty->link->driver_data) |
70 | | - devpts_pty_kill(tty->link->driver_data); |
| 72 | + if (tty->link->driver_data) { |
| 73 | + struct path *path = tty->link->driver_data; |
| 74 | + |
| 75 | + devpts_pty_kill(path->dentry); |
| 76 | + path_put(path); |
| 77 | + kfree(path); |
| 78 | + } |
71 | 79 | mutex_unlock(&devpts_mutex); |
72 | 80 | } |
73 | 81 | #endif |
@@ -440,6 +448,48 @@ static int pty_common_install(struct tty_driver *driver, struct tty_struct *tty, |
440 | 448 | return retval; |
441 | 449 | } |
442 | 450 |
|
| 451 | +/** |
| 452 | + * pty_open_peer - open the peer of a pty |
| 453 | + * @tty: the peer of the pty being opened |
| 454 | + * |
| 455 | + * Open the cached dentry in tty->link, providing a safe way for userspace |
| 456 | + * to get the slave end of a pty (where they have the master fd and cannot |
| 457 | + * access or trust the mount namespace /dev/pts was mounted inside). |
| 458 | + */ |
| 459 | +static struct file *pty_open_peer(struct tty_struct *tty, int flags) |
| 460 | +{ |
| 461 | + if (tty->driver->subtype != PTY_TYPE_MASTER) |
| 462 | + return ERR_PTR(-EIO); |
| 463 | + return dentry_open(tty->link->driver_data, flags, current_cred()); |
| 464 | +} |
| 465 | + |
| 466 | +static int pty_get_peer(struct tty_struct *tty, int flags) |
| 467 | +{ |
| 468 | + int fd = -1; |
| 469 | + struct file *filp = NULL; |
| 470 | + int retval = -EINVAL; |
| 471 | + |
| 472 | + fd = get_unused_fd_flags(0); |
| 473 | + if (fd < 0) { |
| 474 | + retval = fd; |
| 475 | + goto err; |
| 476 | + } |
| 477 | + |
| 478 | + filp = pty_open_peer(tty, flags); |
| 479 | + if (IS_ERR(filp)) { |
| 480 | + retval = PTR_ERR(filp); |
| 481 | + goto err_put; |
| 482 | + } |
| 483 | + |
| 484 | + fd_install(fd, filp); |
| 485 | + return fd; |
| 486 | + |
| 487 | +err_put: |
| 488 | + put_unused_fd(fd); |
| 489 | +err: |
| 490 | + return retval; |
| 491 | +} |
| 492 | + |
443 | 493 | static void pty_cleanup(struct tty_struct *tty) |
444 | 494 | { |
445 | 495 | tty_port_put(tty->port); |
@@ -613,6 +663,8 @@ static int pty_unix98_ioctl(struct tty_struct *tty, |
613 | 663 | return pty_get_pktmode(tty, (int __user *)arg); |
614 | 664 | case TIOCGPTN: /* Get PT Number */ |
615 | 665 | return put_user(tty->index, (unsigned int __user *)arg); |
| 666 | + case TIOCGPTPEER: /* Open the other end */ |
| 667 | + return pty_get_peer(tty, (int) arg); |
616 | 668 | case TIOCSIG: /* Send signal to other side of pty */ |
617 | 669 | return pty_signal(tty, (int) arg); |
618 | 670 | } |
@@ -740,6 +792,7 @@ static int ptmx_open(struct inode *inode, struct file *filp) |
740 | 792 | { |
741 | 793 | struct pts_fs_info *fsi; |
742 | 794 | struct tty_struct *tty; |
| 795 | + struct path *pts_path; |
743 | 796 | struct dentry *dentry; |
744 | 797 | int retval; |
745 | 798 | int index; |
@@ -793,16 +846,26 @@ static int ptmx_open(struct inode *inode, struct file *filp) |
793 | 846 | retval = PTR_ERR(dentry); |
794 | 847 | goto err_release; |
795 | 848 | } |
796 | | - tty->link->driver_data = dentry; |
| 849 | + /* We need to cache a fake path for TIOCGPTPEER. */ |
| 850 | + pts_path = kmalloc(sizeof(struct path), GFP_KERNEL); |
| 851 | + if (!pts_path) |
| 852 | + goto err_release; |
| 853 | + pts_path->mnt = filp->f_path.mnt; |
| 854 | + pts_path->dentry = dentry; |
| 855 | + path_get(pts_path); |
| 856 | + tty->link->driver_data = pts_path; |
797 | 857 |
|
798 | 858 | retval = ptm_driver->ops->open(tty, filp); |
799 | 859 | if (retval) |
800 | | - goto err_release; |
| 860 | + goto err_path_put; |
801 | 861 |
|
802 | 862 | tty_debug_hangup(tty, "opening (count=%d)\n", tty->count); |
803 | 863 |
|
804 | 864 | tty_unlock(tty); |
805 | 865 | return 0; |
| 866 | +err_path_put: |
| 867 | + path_put(pts_path); |
| 868 | + kfree(pts_path); |
806 | 869 | err_release: |
807 | 870 | tty_unlock(tty); |
808 | 871 | // This will also put-ref the fsi |
|
0 commit comments