Subject: bin/3330: Security bug (buffer overflow) in lib/libterm/tgoto.c
To: None <,>
From: None <>
List: netbsd-bugs
Date: 03/14/1997 04:43:04
>Number:         3330
>Category:       bin
>Synopsis:       Security bug (buffer overflow) in lib/libterm/tgoto.c
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Mar 13 18:50:01 1997
>Originator:     Tero Kivinen
SSH Communications Security
>Release:        NetBSD 1.2, FreeBSD 2.1-STABLE

System: NetBSD 1.2 NetBSD 1.2 (TAULU) #50: Mon Feb 24 21:40:52 EET 1997 i386
System: FreeBSD 2.1.5-RELEASE FreeBSD 2.1.5-RELEASE #1: Thu Oct 31 23:58:19 EET 1996  i386


The termcap libraries tgoto function has buffer overflow bug that can
be used to overwrite data in BSS segment.

The tgoto have function have static char result[MAXRETURNSIZE] (64
characters) buffer that is used to return cursor addressing string
from tgoto function. If the CM-cabability have more than 64 characters
in it the tgoto function will overwrite something in the bss segment
after result-variable. There are no checks about the length of cm
string nor checks if the resulting string is longer than MAXRETURNSIZE

For example suid root system utility "systat" is vulnerable to this
bug. No known exploits of this exists yet, but someone might find
such. Also lots of systems have other suid root/kmem etc program
installed that are vulnerable to this bug. For example top and monitor
are such programs. 

Easiest way to demonstrate this is to change TERMCAP environment
variable and add some about 80 characters to cm-capablitie and then
start anything that uses curses or termcap directly (initscr in curses
library will call setterm that will call tgoto to test if we can move
cursor around...).

The fix in netbsd current that changes the strcpy at the end of
tgoto-function to strncpy isn't sufficient, because the dp pointer
might already be way over the length of result buffer thus
"sizeof(result) - (dp - result) - 1" results to negative value.

Notice that fix in netbsd-current also may return non null terminated
string, thus causing unexptected behavior later. 


~> TERMCAP='xterm|vs100|xterm terminal emulator (X11R6 Window System):
	:am:km:mi:ms:xn:xo:     :co#80:it#8:li#25:
	:se=\E[m:sf=^J:so=\E[7m:sr=\EM:ta=^I:   :ue=\E[m:up=\E[A:
~> export TERMCAP
~> systat
zsh: 27863 bus error  systat


Add check that result buffer isn't overflown.
--		              	     Work : +358-9-4354 3205
Magnus Enckellin kuja 9 K 19, 02610, Espoo   Home : +358-9-502 1573