summaryrefslogtreecommitdiff
path: root/security/ssh/files/patch-xa
blob: a775ff6820da6aaea4c8423713a2b024a161a4c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
Note that this patch has been incorporated into the port due to problems
with patching a autoconf generated configure script. The script itself contains
linenumbers and in case of two patches against that script the second one fails
because it expects something that the first patch has already changed. The
only clean way is to re-generate it with autoconf. *sigh*
This patch was fetched from
http://www.ssh.org/patches/patch-ssh-1.2.27-bsd.tty.chown
  - torstenb@FreeBSD.org, Tue Jan 11 21:36:46 CET 2000


Patch for problem with tty ownership with chflags and chown in BSD 4.4
variants. Fixes a security bug in tty allocation. 

This patch works for ssh-1.2.27.

Apply with the following commands:

% cd /wherever/you/hold/your/sources/ssh-1.2.27
% patch -p1 -l < /path/to/where/you/saved/patch-ssh-1.2.27-bsd.tty.chown
% ./configure --whatever-config-flags-you-use
% make clean
% make
% su
Password: ***********
# make install
# kill -HUP `cat /var/run/sshd.pid`

You should be all set.

Sami Lehtinen <sjl@ssh.fi>

--begin patch--
diff -u --recursive -X /u/sjl/bin/diff-src-db auth-passwd.c.orig auth-passwd.c
--- auth-passwd.c.orig	Wed May 12 14:19:23 1999
+++ auth-passwd.c	Wed Aug 11 19:49:32 1999
@@ -613,7 +613,13 @@
             /* get_name pulls out just the name not the
                type */
               strcpy(ccname + 5, krb5_cc_get_name(ssh_context, ccache));
-              (void) chown(ccname + 5, pw->pw_uid, pw->pw_gid);
+              if (chown(ccname + 5, pw->pw_uid, pw->pw_gid) < 0)
+                {
+                  log_msg("Kerberos: chown failed for %s, error: %s",
+                          ccname + 5, strerror(errno));
+                  packet_send_debug("Kerberos: chown failed for %s", ccname + 5);
+                  goto errout;
+                }
               
               /* If tgt was passed unlink file */
               if (ticket)
diff -u --recursive -X /u/sjl/bin/diff-src-db config.h.in.orig config.h.in
--- config.h.in.orig	Wed May 12 14:20:04 1999
+++ config.h.in	Wed Aug 11 20:20:51 1999
@@ -360,6 +360,9 @@
 /* Define if you have the authenticate function.  */
 #undef HAVE_AUTHENTICATE
 
+/* Define if you have the chflags function.  */
+#undef HAVE_CHFLAGS
+
 /* Define if you have the clock function.  */
 #undef HAVE_CLOCK
 
diff -u --recursive -X /u/sjl/bin/diff-src-db configure.in.orig configure.in
--- configure.in.orig	Wed May 12 14:20:02 1999
+++ configure.in	Wed Aug 11 20:05:13 1999
@@ -433,6 +433,7 @@
 AC_CHECK_FUNCS(strchr memcpy setlogin openpty _getpty clock fchmod ulimit)
 AC_CHECK_FUNCS(gethostname getdtablesize umask innetgr initgroups setpgrp)
 AC_CHECK_FUNCS(setpgid daemon waitpid ttyslot authenticate getpt isastream)
+AC_CHECK_FUNCS(chflags)
 
 AC_REPLACE_FUNCS(strerror memmove remove random putenv crypt socketpair snprintf)
 
diff -u --recursive -X /u/sjl/bin/diff-src-db sshd.c.orig sshd.c
--- sshd.c.orig	Wed May 12 14:19:29 1999
+++ sshd.c	Wed Aug 11 20:26:31 1999
@@ -2897,9 +2897,87 @@
               tty_mode = S_IRUSR|S_IWUSR|S_IWGRP|S_IWOTH;
             }
 
+        retry_chown:
+
           /* Change ownership of the tty. */
-          (void)chown(ttyname, pw->pw_uid, tty_gid);
-          (void)chmod(ttyname, tty_mode);
+          if (chown(ttyname, pw->pw_uid, tty_gid) < 0)
+            {
+              /* chown failed. Atleast two possibilities. Either we are not
+                 running as root, in which case this is OK, or we are running
+                 on BSD, and somebody has put some flags to the tty. */
+              
+              /* Check whether we are root or not.*/
+              if (getuid() != UID_ROOT)
+                {
+                  /* We are not, and then this is OK. */
+                  debug("chown failed (but we're not root anyway) for "
+                        "%s, error %s", ttyname, strerror(errno));
+                }
+              else
+                {
+#ifdef HAVE_CHFLAGS
+                  static int retrying = 0;
+                  struct stat st;
+                  
+                  if (!retrying)
+                    {
+                      debug("chown failed for %s, error: %s. Removing "     
+                            "user-settable flags, and retrying.",
+                            ttyname, strerror(errno));
+                      
+                      if (stat(ttyname, &st) < 0)
+                        {
+                          error("stat failed for %s, error: %s",
+                                ttyname, strerror(errno));
+                        }
+                      else
+                        {              
+                          debug("Removing user-settable flags with "
+                                "chflags.");
+                          /* Remove user definable flags. */
+                          if (chflags(ttyname, st.st_flags &
+                                      ~(UF_NODUMP | UF_IMMUTABLE |
+                                        UF_APPEND | UF_OPAQUE)) < 0)
+                            {
+                              debug("chflags failed for %s, error: %s",
+                                    ttyname, strerror(errno));
+                            }
+                          else
+                            {
+                              debug("Retrying...");
+                              retrying = 1;
+                              goto retry_chown;
+                            }
+                        }
+                    }
+                  else
+                    {
+                      debug("chown failed even with retry. error: %s",
+                            strerror(errno));
+                    }
+                  
+#endif /* HAVE_CHFLAGS */          
+                  error("ssh_pty_allocate_and_fork: chown failed for %s.",
+                        ttyname);
+                  goto fail;
+                }
+            }
+          
+          if (chmod(ttyname, tty_mode) < 0)
+            {
+              if (getuid() != UID_ROOT)
+                {
+                  /* We are not, and then this is (probably) OK. */
+                  debug("chmod failed (but we're not root anyway) for "
+                        "%s, error %s", ttyname, strerror(errno));
+                }
+              else
+                {
+                  error("ssh_pty_allocate_and_fork: chmod %s: %s",
+                        ttyname, strerror(errno));
+                  goto fail;
+                }
+            }
 
           /* Get TERM from the packet.  Note that the value may be of arbitrary
              length. */