Third argument of readlink does not
leave space for null terminator in buffer
This defect occurs
when you pass a buffer size argument to readlink() that
does not leave space for a null terminator in the buffer.
For instance:
ssize_t len = readlink("/usr/bin/perl", buf, sizeof(buf));readlink() does
not leave space to enter a null terminator.The readlink() function copies the content
of a symbolic link (first argument) to a buffer (second argument).
However, the function does not append a null terminator to the copied
content. After using readlink(), you must explicitly
add a null terminator to the buffer.
If you fill the entire buffer when using readlink,
you do not leave space for this null terminator.
When using the readlink() function, make
sure that the third argument is one less than the buffer size.
Then, append a null terminator to the buffer. To determine where
to add the null terminator, check the return value of readlink().
If the return value is -1, an error has occurred. Otherwise, the return
value is the number of characters (bytes) copied.
| Group: Security |
| Language: C | C++ |
| Default: Off |
Command-Line Syntax: READLINK_MISUSE |
| Impact: Medium |
| CWE ID: 170 |
Array access out of bounds | File access between time of check and use (TOCTOU) | Invalid use of standard library string routine | Pointer access out of bounds | Returned value of a sensitive function not checked