Mr. Hornik has discovered buffer overflow vulnerability in slocate version 2.6. Many Linux distributions have their slocate package based on this version. Patrik found at least RedHat package to be vulnerable. The vulnerability corrupts heap management structures and possibly leads to gaining slocate group privileges, which allows reading global slocate database and thus obtaining list of all files in the system by unauthorized user.
Program slocate works on user supplied database with setgid to slocate group. With user prepared slocate database one can cause (Patrik is referring to source lines from slocate-2.6-1.src.rpm from RH 7.3) that pathlen after executing main.c:1255 will have value -1. It must be caused by not the first path in the database because it is verified in validate_db. Then on line main.c:1275 the last byte of memory block header (this memory block size) will be overwritten with user supplied value. The codedpath is never freed by the code, but it is possible to trigger realloc on line 1269 later by data in database.
Because of not freeing some dynamic memory, using multiple databases and multiple search patterns it should be possible to prepare heap before triggering this vulnerability to allow later execution of arbitrary code, thus gaining slocate group privileges. This allows reading of global slocate database with list of all files in the system by unauthorized user. The exploit is not available at this time.
Suggested and correct patch is to change condition on line 1263 to pathlen <= 0.
Who is affected?
Affected are all RedHat distributions up to version 9.0 including.
slocate version 2.6 and below is vulnerable. slocate version 2.7 and all packages based on this version are not vulnerable.
Recommendations
Patrik recommends to upgrade slocate package to the fixed version.
If obtaining the list of all files on the system by unauthorized user is security risk for your system Patrik recommends to remove slocate database and disable automatic generation of this database (as daily cron job) or remove slocate utility or generate database only from safe files until fixed version is installed.
Exploitation:
The exploitation which allows overwriting memory management data of the heap was described in original advisory. Patrik is referring to source lines from slocate-2.6-1.src.rpm from RH 7.3 here too. The attached source code constants are for RH 7.3 on i686 on 2003/10/10, except that some parts of them are * here.
The main idea behind the exploitation is not straightforward, so I describe it below. The attached source code prepares test.db with following properties. It exploits the bug by: slocate -i -d test.db `perl -e 'print "B"x1024'`
1) We overwrite one byte of the buffer management headers the overflow allows to overwrite - it is highest byte of size of memory block of codedpath. We will trigger realloc on main.c:1269 later.
2) We are playing here with codedpath, casestr and bucket_of_holding. They are allocated in this order. When casestr is big enough, it is placed on the heap after codedpath. By big enough database bucket_of_holding is reallocated to 0x4002**** region later.
3) We need to prepare the area where the codedpath ends according to overwriten block size so chunk_free called from chunk_realloc will not fail because of accessing inaccessible memory or finding bad values there. Only the highest byte of memory block size can be changed, so the size change will be multiply of 0x1000000. The needed data (two small blocks just after overwritten codepath, first memory block marked as used) are placed on 0x400***** by having them on appropriate position in database. The change in size is 0x38 * 0x1000000.
4) We trigger realloc on main.c:1269, so overwritten block size value is used. We trigger realloc by prepare path which is longer than initial size (4096 bytes). We are reallocating to size that is smaller than the overwritten size and big enough to end after casestr ends.
5) Then we can overwrite casestr and memory after it by writing to codedpath because memory management thinks codedpath is so long. We will overwrite the size of memory block of casestr to 0x508 (from 0x408). Later free(casestr) is called and it seems it is top in its memory area so free behaves differently than we want. We change it so comparison of address of next block with top simply fails.
6) We place fake memory blocks after casestr such that free(casestr) overwrites address of close() in GOT to point to our arbitrary code (by setting fake backward and forward pointers in next (free) block after casestr). On main.c:1357 close(fd) is called and our code gets the control. We setregid(slocate,slocate) and run the shell. That is it.