1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

For #1229: Check the return value of vsnprintf.

This commit is contained in:
winlin 2022-08-08 08:22:02 +08:00
parent 079ac107f0
commit 5ae495ab95
4 changed files with 94 additions and 32 deletions

View file

@ -97,10 +97,15 @@ void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* f
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
write_log(fd, log_data, size, SrsLogLevelVerbose); write_log(fd, log_data, size, SrsLogLevelVerbose);
} }
@ -119,10 +124,15 @@ void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt,
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
write_log(fd, log_data, size, SrsLogLevelInfo); write_log(fd, log_data, size, SrsLogLevelInfo);
} }
@ -141,10 +151,15 @@ void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
write_log(fd, log_data, size, SrsLogLevelTrace); write_log(fd, log_data, size, SrsLogLevelTrace);
} }
@ -163,10 +178,15 @@ void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt,
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
write_log(fd, log_data, size, SrsLogLevelWarn); write_log(fd, log_data, size, SrsLogLevelWarn);
} }
@ -185,14 +205,25 @@ void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
// add strerror() to error msg. // add strerror() to error msg.
// Check size to avoid security issue https://github.com/ossrs/srs/issues/1229 // Check size to avoid security issue https://github.com/ossrs/srs/issues/1229
if (errno != 0 && size < LOG_MAX_SIZE) { if (errno != 0 && size < LOG_MAX_SIZE) {
size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno)); r0 = snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno));
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= LOG_MAX_SIZE - size) {
return;
}
size += r0;
} }
write_log(fd, log_data, size, SrsLogLevelError); write_log(fd, log_data, size, SrsLogLevelError);

View file

@ -49,7 +49,7 @@ public:
{ {
static char buf[4096] = {0}; static char buf[4096] = {0};
int nbytes = vsnprintf(buf, sizeof(buf), fmt, vl); int nbytes = vsnprintf(buf, sizeof(buf), fmt, vl);
if (nbytes > 0) { if (nbytes > 0 && nbytes < sizeof(buf)) {
// Srs log is always start with new line, replcae '\n' to '\0', make log easy to read. // Srs log is always start with new line, replcae '\n' to '\0', make log easy to read.
if (buf[nbytes - 1] == '\n') { if (buf[nbytes - 1] == '\n') {
buf[nbytes - 1] = '\0'; buf[nbytes - 1] = '\0';

View file

@ -101,7 +101,7 @@ SrsCplxError* SrsCplxError::create(const char* func, const char* file, int line,
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
static char buffer[4096]; static char buffer[4096];
vsnprintf(buffer, sizeof(buffer), fmt, ap); int r0 = vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap); va_end(ap);
SrsCplxError* err = new SrsCplxError(); SrsCplxError* err = new SrsCplxError();
@ -111,7 +111,9 @@ SrsCplxError* SrsCplxError::create(const char* func, const char* file, int line,
err->line = line; err->line = line;
err->code = code; err->code = code;
err->rerrno = rerrno; err->rerrno = rerrno;
err->msg = buffer; if (r0 > 0 && r0 < sizeof(buffer)) {
err->msg = string(buffer, r0);
}
err->wrapped = NULL; err->wrapped = NULL;
if (_srs_context) { if (_srs_context) {
err->cid = _srs_context->get_id(); err->cid = _srs_context->get_id();
@ -126,7 +128,7 @@ SrsCplxError* SrsCplxError::wrap(const char* func, const char* file, int line, S
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
static char buffer[4096]; static char buffer[4096];
vsnprintf(buffer, sizeof(buffer), fmt, ap); int r0 = vsnprintf(buffer, sizeof(buffer), fmt, ap);
va_end(ap); va_end(ap);
SrsCplxError* err = new SrsCplxError(); SrsCplxError* err = new SrsCplxError();
@ -138,7 +140,9 @@ SrsCplxError* SrsCplxError::wrap(const char* func, const char* file, int line, S
err->code = v->code; err->code = v->code;
} }
err->rerrno = rerrno; err->rerrno = rerrno;
err->msg = buffer; if (r0 > 0 && r0 < sizeof(buffer)) {
err->msg = string(buffer, r0);
}
err->wrapped = v; err->wrapped = v;
if (_srs_context) { if (_srs_context) {
err->cid = _srs_context->get_id(); err->cid = _srs_context->get_id();

View file

@ -134,10 +134,15 @@ void SrsConsoleLog::verbose(const char* tag, SrsContextId context_id, const char
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
@ -154,10 +159,15 @@ void SrsConsoleLog::info(const char* tag, SrsContextId context_id, const char* f
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
@ -174,10 +184,15 @@ void SrsConsoleLog::trace(const char* tag, SrsContextId context_id, const char*
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stdout, "%s\n", buffer); fprintf(stdout, "%s\n", buffer);
} }
@ -194,10 +209,15 @@ void SrsConsoleLog::warn(const char* tag, SrsContextId context_id, const char* f
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
fprintf(stderr, "%s\n", buffer); fprintf(stderr, "%s\n", buffer);
} }
@ -214,13 +234,24 @@ void SrsConsoleLog::error(const char* tag, SrsContextId context_id, const char*
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
// we reserved 1 bytes for the new line. int r0 = vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
size += vsnprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, fmt, ap);
va_end(ap); va_end(ap);
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
// add strerror() to error msg. // add strerror() to error msg.
if (errno != 0) { if (errno != 0) {
size += snprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, "(%s)", strerror(errno)); r0 = snprintf(buffer + size, SRS_BASIC_LOG_SIZE - size, "(%s)", strerror(errno));
// Something not expected, drop the log.
if (r0 <= 0 || r0 >= SRS_BASIC_LOG_SIZE - size) {
return;
}
size += r0;
} }
fprintf(stderr, "%s\n", buffer); fprintf(stderr, "%s\n", buffer);
@ -277,11 +308,7 @@ bool srs_log_header(char* buffer, int size, bool utc, bool dangerous, const char
// Exceed the size, ignore this log. // Exceed the size, ignore this log.
// Check size to avoid security issue https://github.com/ossrs/srs/issues/1229 // Check size to avoid security issue https://github.com/ossrs/srs/issues/1229
if (written >= size) { if (written <= 0 || written >= size) {
return false;
}
if (written == -1) {
return false; return false;
} }