Skip to content

Commit

Permalink
skip pty chown if it is group owned by tty or nogroup
Browse files Browse the repository at this point in the history
  • Loading branch information
rd235 committed Jan 22, 2024
1 parent aa37404 commit 7197037
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/sshpty.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,17 @@ pty_setowner(struct passwd *pw, const char *tty_name)
{
struct group *grp;
gid_t gid;
gid_t nogroup_gid;
mode_t mode;
struct stat st;

/* get nogroup's gid */
grp = getgrnam("nogroup");
if (grp)
nogroup_gid = grp->gr_gid;
else
nogroup_gid = -1;

/* Determine the group to make the owner of the tty. */
grp = getgrnam("tty");
if (grp) {
Expand All @@ -382,7 +390,8 @@ pty_setowner(struct passwd *pw, const char *tty_name)

/* Allow either "tty" gid or user's own gid. On Linux with openpty()
* this varies depending on the devpts mount options */
if (st.st_uid != pw->pw_uid || !(st.st_gid == gid || st.st_gid == pw->pw_gid)) {
if (st.st_uid != pw->pw_uid ||
!(st.st_gid == gid || st.st_gid == nogroup_gid || st.st_gid == pw->pw_gid)) {
if (chown(tty_name, pw->pw_uid, gid) < 0) {
if (errno == EROFS &&
(st.st_uid == pw->pw_uid || st.st_uid == 0)) {
Expand Down

0 comments on commit 7197037

Please sign in to comment.