FORTIFY_SOURCE: Add openat, fix bug

Add fortify_source support for openat(). This change requires that
an argument be supplied when using O_CREAT.

Fix unnecessary call to __open_2. If, at compile time, we know that
"flags" is constant and DOESN'T contain O_CREAT, the call to __open_2
is useless.

Change-Id: Ifcd29c4fb25e25656961d7552d672e161f0cfdbd
This commit is contained in:
Nick Kralevich 2012-07-02 12:24:42 -07:00
parent 2ddf77b377
commit a3e230d1fa
2 changed files with 45 additions and 7 deletions

View File

@ -59,10 +59,10 @@ extern int creat(const char* path, mode_t mode);
* http://clang.llvm.org/docs/UsersManual.html#c_unimpl_gcc
*/
extern void __open_creat_error()
__attribute__((__error__ ("open called with O_CREAT, but missing mode")));
extern void __open_toomanyargs_error()
__attribute__((__error__ ("open called with too many arguments")));
extern void __creat_error()
__attribute__((__error__ ("called with O_CREAT, but missing mode")));
extern void __too_many_args_error()
__attribute__((__error__ ("too many arguments")));
extern int __open_real(const char *pathname, int flags, ...)
__asm__(__USER_LABEL_PREFIX__ "open");
extern int __open_2(const char *, int);
@ -71,21 +71,44 @@ __BIONIC_FORTIFY_INLINE
int open(const char *pathname, int flags, ...) {
if (__builtin_constant_p(flags)) {
if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
__open_creat_error(); // compile time error
__creat_error(); // compile time error
}
}
if (__builtin_va_arg_pack_len() > 1) {
__open_toomanyargs_error(); // compile time error
__too_many_args_error(); // compile time error
}
if (__builtin_va_arg_pack_len() == 0) {
if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
return __open_2(pathname, flags);
}
return __open_real(pathname, flags, __builtin_va_arg_pack());
}
extern int __openat_2(int, const char *, int);
extern int __openat_real(int dirfd, const char *pathname, int flags, ...)
__asm__(__USER_LABEL_PREFIX__ "openat");
__BIONIC_FORTIFY_INLINE
int openat(int dirfd, const char *pathname, int flags, ...) {
if (__builtin_constant_p(flags)) {
if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
__creat_error(); // compile time error
}
}
if (__builtin_va_arg_pack_len() > 1) {
__too_many_args_error(); // compile time error
}
if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
return __openat_2(dirfd, pathname, flags);
}
return __openat_real(dirfd, pathname, flags, __builtin_va_arg_pack());
}
#endif /* !defined(__clang__) */
#endif /* defined(__BIONIC_FORTIFY_INLINE) */

View File

@ -28,6 +28,8 @@
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <private/logd.h>
extern int __openat(int, const char*, int, int);
@ -49,3 +51,16 @@ int openat(int fd, const char *pathname, int flags, ...)
return __openat(fd, pathname, flags, mode);
}
int __openat_2(int fd, const char *pathname, int flags)
{
if (flags & O_CREAT) {
__libc_android_log_print(ANDROID_LOG_FATAL, "libc",
"*** openat(O_CREAT) called without specifying a mode ***\n");
abort();
}
flags |= O_LARGEFILE;
return __openat(fd, pathname, flags, 0);
}