Bug 1472485

Summary: libselinux realpath_not_final calls free() on invalid pointer
Product: Red Hat Enterprise Linux 7 Reporter: Ivan Agarkov <ivan.agarkov>
Component: libselinuxAssignee: Petr Lautrbach <plautrba>
Status: CLOSED DUPLICATE QA Contact: Milos Malik <mmalik>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 7.2CC: dwalsh, lvrabec, mgrepl, mmalik, plautrba, ssekidde, vmojzis
Target Milestone: rc   
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2017-07-19 08:30:19 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Ivan Agarkov 2017-07-18 21:38:09 UTC
Description of problem:
libselinux matchpathcon() calls realpath_not_final() function if the path is a symlink.
realpath_not_final doesn't track double slash properly that caused to free() on invalid pointer.


How reproducible:
always

Steps to Reproduce:
1. matchpathcon /bin ( it works )
2. matchpathcon //bin ( it crashes )
3. or compile and run the following code:
#include <selinux/selinux.h>
#include <sys/stat.h>
#include <stdlib.h>

int main() {
    char **conptr = malloc(sizeof(char *));
    struct stat st;
    lstat("//bin", &st);
    matchpathcon("//bin", st.st_mode, conptr);
    return 0;
}

Actual results:
*** Error in `./test': free(): invalid pointer: 0x0000000000b12f41 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x7c503)[0x7feac6bd3503]
/lib64/libselinux.so.1(realpath_not_final+0xc0)[0x7feac6f2dd50]
/lib64/libselinux.so.1(matchpathcon+0xce)[0x7feac6f2deee]
./test[0x400770]
/lib64/libc.so.6(__libc_start_main+0xf5)[0x7feac6b78b35]
./test[0x400669]

Expected results:
//bin system_u:object_r:bin_t

Additional info:

I've tested latest master branch of libselinux and looks like the bug was fixed there.

Comment 2 Milos Malik 2017-07-19 08:18:14 UTC
This bug is reproducible on RHEL-7.3, but it is not reproducible on RHEL-7.4.

I believe this bug is a duplicate of BZ#1404644.

Comment 3 Vit Mojzis 2017-07-19 08:30:19 UTC

*** This bug has been marked as a duplicate of bug 1404644 ***