Fix fortify

Close #7009, fix #7003
This commit is contained in:
topjohnwu 2023-05-23 16:31:24 -07:00
parent 5627053b74
commit 69144942e3
2 changed files with 9 additions and 5 deletions

View File

@ -140,5 +140,5 @@ int __openat_2(int fd, const char* pathname, int flags) {
int __vsnprintf_chk(char* dst, size_t supplied_size, int /*flags*/, int __vsnprintf_chk(char* dst, size_t supplied_size, int /*flags*/,
size_t dst_len_from_compiler, const char* format, va_list va) { size_t dst_len_from_compiler, const char* format, va_list va) {
__check_buffer_access("vsnprintf", "write into", supplied_size, dst_len_from_compiler); __check_buffer_access("vsnprintf", "write into", supplied_size, dst_len_from_compiler);
return vsnprintf(dst, supplied_size, format, va); return __call_bypassing_fortify(vsnprintf)(dst, supplied_size, format, va);
} }

View File

@ -8,11 +8,15 @@
using namespace std; using namespace std;
#undef vsnprintf
static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) { static int fmt_and_log_with_rs(LogLevel level, const char *fmt, va_list ap) {
char buf[4096]; constexpr int sz = 4096;
int ret = vssprintf(buf, sizeof(buf), fmt, ap); char buf[sz];
log_with_rs(level, rust::Slice(reinterpret_cast<const uint8_t *>(buf), ret)); buf[0] = '\0';
return ret; // Fortify logs when a fatal error occurs. Do not run through fortify again
int len = std::min(__call_bypassing_fortify(vsnprintf)(buf, sz, fmt, ap), sz - 1);
log_with_rs(level, rust::Slice(reinterpret_cast<const uint8_t *>(buf), len));
return len;
} }
// Used to override external C library logging // Used to override external C library logging