Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2023-12-30
23:59
(cherry-pick) Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Close... check-in: 81c13398d0 user: jan.nijtmans tags: core-8-6-branch
21:32
(cherry-pick) Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Close... check-in: 08ffba30e8 user: jan.nijtmans tags: core-8-branch
2018-06-13
16:20 • Ticket [0ac9d06895] Don't call getsockname(2) in Tcl_MakeFileChannel(3) unle... artifact: abf17c3476 user: stu
16:18 • Closed ticket [0ac9d06895]. artifact: d481860082 user: stu
16:15
Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Closes RFE [0ac9d06... check-in: a23a536a48 user: stu tags: trunk
2018-06-07
06:28 • Ticket [0ac9d06895] Don't call getsockname(2) in Tcl_MakeFileChannel(3) unle... artifact: cf4e37a582 user: stu
06:28 • Ticket [0ac9d06895]: 4 changes artifact: a7b7e1ebfe user: stu
06:27 • Add attachment patch-unix_tclUnixChan_c to ticket [0ac9d06895f000b3|0ac... artifact: fd56a6851b user: stu
06:25 • New ticket [0ac9d06895] Don't call getsockname(2) in Tcl_MakeFileChannel(3) ... artifact: e70fbc08a0 user: stu

Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary

Closed / Accepted0ac9d06895f000b3c83fcb6b14940acc79f11122Created 8 years agoUpdated 8 years ago
Descriptionhtml2018-06-07 06:25:47

Tcl_MakeFileChannel(3) checks if the given fd is a tty or a socket before assuming it's a file.
If there are pledge(2)es in effect that are restricting the usage of network functions then Tcl_MakeFileChannel will segfault when it tries to use getsockname(2) to check if the fd is a socket.
An important time when this happens is when tclsh is fed a script on stdin: tclsh will immediately segfault.
By using fstat(2) to first check if the fd is a socket, a call to getsockname might be avoided.
This may have security implications.

Attached is a patch against 8.6.8.
The patch has the nice side effect of putting all the socket bits in one place.

Here is a test program:

#include <unistd.h>
#define TCLSH "/usr/local/bin/tclsh"
int main (int argc, char *argv[]) {
        char *a[] = { TCLSH, NULL };
        char *e[] = { NULL };

#ifdef DNS
        pledge(NULL, "stdio rpath getpw dns");
#else
# ifdef INET
        pledge(NULL, "stdio rpath getpw inet");
# else
        pledge(NULL, "stdio rpath getpw");
# endif
#endif

        execve(TCLSH, a, e);
        return 0;
}
Build with no -D's:
$ cc -o tpl tpl.c
$ echo 'puts moo' | ./tpl
Abort trap (core dumped) 
Build with either -DINET or -DDNS:
$ cc -o tpl tpl.c -DINET
$ echo 'puts moo' | ./tpl
moo

Referenced by3
  • 81c13398d0(cherry-pick) Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Closes RFE [0ac9d06895]. Permits better constraining of Tcl/tclsh via OpenBSD's pledge(2) or similar mechanisms. Minor rewrite check-in2023-12-30 23:59:43
  • 08ffba30e8(cherry-pick) Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Closes RFE [0ac9d06895]. Permits better constraining of Tcl/tclsh via OpenBSD's pledge(2) or similar mechanisms. Minor rewrite check-in2023-12-30 21:32:22
  • a23a536a48Don't call getsockname(2) in Tcl_MakeFileChannel(3) unless absolutely necessary. Closes RFE [0ac9d06895]. Permits better constraining of Tcl/tclsh via OpenBSD's pledge(2) or similar mechanisms. check-in2018-06-13 16:15:27
stu Ticket [0ac9d06895]: 4 changes [History diff]2018-06-07 06:28:26
stu Ticket [0ac9d06895]: 3 changes [History diff]2018-06-07 06:28:40
stuplain2018-06-13 16:18:24
Closed by [a23a536a48].
stuwiki2018-06-13 16:20:03
Log in (anonymous is fine) to add a comment.

Attachments: