bkloppenborg / pathfind Goto Github PK
View Code? Open in Web Editor NEWC++ library for finding the path of the current executable.
License: GNU Lesser General Public License v3.0
C++ library for finding the path of the current executable.
License: GNU Lesser General Public License v3.0
Line 88 in d822a2f
__gnu_linux__
isn't defined on Alpine, just __linux__
.
Hi,
on many systems, the current implementation of do_readlink
may cause a random read into uninitialized memory.
do_readlink
currently directly converts the return value of readlink
into a std::string
. However, the return of readlink
is not null-terminated. Hence, when you don't use the length returned by readlink
and have a system that does not pre-initialize a buffer with 0-bytes, memory will be read until randomly encountering a null-byte.
Reproducible with this program:
#include <iostream>
#include <PathFind.hpp>
int main()
{
std::cout << do_readlink("./test") << std::endl;
return 0;
}
With linking ./test to /, on ubuntu 20.04 with gcc 9.3.0 this yields output like:
$./a.out
/��GT�
Valgrind output:
$ valgrind ./a.out
==2418761== Memcheck, a memory error detector
==2418761== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2418761== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==2418761== Command: ./a.out
==2418761==
==2418761== Conditional jump or move depends on uninitialised value(s)
==2418761== at 0x10A865: std::char_traits<char>::length(char const*) (in .../pathfind/src/a.out)
==2418761== by 0x10A978: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&) (in .../pathfind/src/a.out)
==2418761== by 0x10A6CE: do_readlink(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (in .../pathfind/src/a.out)
==2418761== by 0x10AC52: main (in .../pathfind/src/a.out)
==2418761==
==2418761== Conditional jump or move depends on uninitialised value(s)
==2418761== at 0x4AE3F5D: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:783)
==2418761== by 0x4AE51A3: _IO_default_xsputn (genops.c:399)
==2418761== by 0x4AE51A3: _IO_default_xsputn (genops.c:370)
==2418761== by 0x4AE2879: _IO_new_file_xsputn (fileops.c:1265)
==2418761== by 0x4AE2879: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1197)
==2418761== by 0x4AD6540: fwrite (iofwrite.c:39)
==2418761== by 0x49879B3: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==2418761== by 0x10AC65: main (in .../pathfind/src/a.out)
==2418761==
==2418761== Syscall param write(buf) points to uninitialised byte(s)
==2418761== at 0x4B611E7: write (write.c:26)
==2418761== by 0x4AE200C: _IO_file_write@@GLIBC_2.2.5 (fileops.c:1181)
==2418761== by 0x4AE3AD0: new_do_write (fileops.c:449)
==2418761== by 0x4AE3AD0: _IO_new_do_write (fileops.c:426)
==2418761== by 0x4AE3AD0: _IO_do_write@@GLIBC_2.2.5 (fileops.c:423)
==2418761== by 0x4AE4012: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:784)
==2418761== by 0x4987478: std::ostream::put(char) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==2418761== by 0x49876A7: std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==2418761== by 0x10AC7A: main (in .../pathfind/src/a.out)
==2418761== Address 0x4da7c81 is 1 bytes inside a block of size 1,024 alloc'd
==2418761== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==2418761== by 0x4AD4E83: _IO_file_doallocate (filedoalloc.c:101)
==2418761== by 0x4AE504F: _IO_doallocbuf (genops.c:347)
==2418761== by 0x4AE40AF: _IO_file_overflow@@GLIBC_2.2.5 (fileops.c:745)
==2418761== by 0x4AE2834: _IO_new_file_xsputn (fileops.c:1244)
==2418761== by 0x4AE2834: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1197)
==2418761== by 0x4AD6540: fwrite (iofwrite.c:39)
==2418761== by 0x49879B3: std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long) (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28)
==2418761== by 0x10AC65: main (in .../pathfind/src/a.out)
==2418761==
/
==2418761==
==2418761== HEAP SUMMARY:
==2418761== in use at exit: 0 bytes in 0 blocks
==2418761== total heap usage: 2 allocs, 2 frees, 73,728 bytes allocated
==2418761==
==2418761== All heap blocks were freed -- no leaks are possible
==2418761==
==2418761== Use --track-origins=yes to see where uninitialised values come from
==2418761== For lists of detected and suppressed errors, rerun with: -s
==2418761== ERROR SUMMARY: 4 errors from 3 contexts (suppressed: 0 from 0)
This also means that the output of any function using do_readlink may be incorrect.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.