Bug 64164

Summary: Buffer overflow in /usr/bin/indent
Product: Base System Reporter: Claes M Nyberg <md0claes>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.1-RELEASE   
Hardware: Any   
OS: Any   

Description Claes M Nyberg 2004-03-12 15:30:22 UTC
/usr/bin/indent crashes with a long HOME environmental variable.
Line 171 in args.c: sprintf(fname, "%s/%s", getenv("HOME"), prof);

Other locations that lacks boundary checking (which might, or might not crash the program):

cd /usr/src/usr.bin/indent
$ iseec -v *.c
Parsing 'args.c'
Parsing 'indent.c'
Parsing 'io.c'
Parsing 'lexi.c'
Parsing 'parse.c'
Parsing 'pr_comment.c'
Function "main" found on line 73 in file indent.c
Found 19 global and 5 private functions in 6 files
(iseec) audit efuncs -vvvv
Using sprintf
Using strcpy
Using creat
Using getc
Using getenv
(iseec) audit efuncs
Using sprintf
Using strcpy
(iseec) calls --to sprintf -a
Function 'sprintf' not found in global table
Found function 'sprintf' in external table
[indent.c:501] main -> sprintf
[indent.c:572] main -> sprintf 
[indent.c:914] main -> sprintf 
[args.c:171] set_profile -> sprintf
[indent.c:1154] bakcopy -> sprintf
(iseec) print indent.c -s 501 -l1
    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
(iseec) print indent.c -s 572 -l1
    sprintf(e_code, "\n.Du %dp+\200p \"%s\"\n", dec_ind * 7, token);
(iseec) print indent.c -s 914 -l1
    sprintf(e_code, "\n.De %dp+\200p\n", dec_ind * 7);
(iseec) print args.c -s 171 -l1
    sprintf(fname, "%s/%s", getenv("HOME"), prof);
(iseec) print indent.c -s 1154
    sprintf(bakfile, "%s.BAK", p);
(iseec)

Fix: 

Use snprintf(3) instead of sprintf(3).
How-To-Repeat: $ export HOME=`perl -e'{ print "A"x8192; }'`
$ gdb `which indent`
GNU gdb 5.2.1 (FreeBSD)
Copyright 2002 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "i386-undermydesk-freebsd"...
(no debugging symbols found)...
(gdb) r
Starting program: /usr/bin/indent
(no debugging symbols found)...(no debugging symbols found)...
Program received signal SIGSEGV, Segmentation fault.
0x41414141 in ?? ()
(gdb)
Comment 1 David Schultz freebsd_committer freebsd_triage 2004-09-19 21:35:12 UTC
State Changed
From-To: open->closed

Fixed, thanks!