r22420 - trunk/stream/stream_dvb.c
Author: nicodvb Date: Sat Mar 3 14:10:31 2007 New Revision: 22420 Modified: trunk/stream/stream_dvb.c Log: replaced 2 instances of sprintf() with snprintf() and one instance of strncpy() followed by string termination with strlcpy(); patch by njkain gmail com. This patch is intended to render code easier to audit against buffer oveflows Modified: trunk/stream/stream_dvb.c ============================================================================== --- trunk/stream/stream_dvb.c (original) +++ trunk/stream/stream_dvb.c Sat Mar 3 14:10:31 2007 @@ -176,8 +176,7 @@ static dvb_channels_list *dvb_get_channe ptr->name = (char*) malloc(k+1); if(! ptr->name) continue; - strncpy(ptr->name, line, k); - ptr->name[k] = 0; + strlcpy(ptr->name, line, k+1); } else continue; @@ -764,7 +763,7 @@ dvb_config_t *dvb_get_config(void) conf->cards = NULL; for(i=0; i<MAX_CARDS; i++) { - sprintf(filename, "/dev/dvb/adapter%d/frontend0", i); + snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend0", i); fd = open(filename, O_RDONLY|O_NONBLOCK); if(fd < 0) { @@ -824,7 +823,7 @@ dvb_config_t *dvb_get_config(void) conf->cards[conf->count].devno = i; conf->cards[conf->count].list = list; conf->cards[conf->count].type = type; - sprintf(name, "DVB-%c card n. %d", type==TUNER_TER ? 'T' : (type==TUNER_CBL ? 'C' : 'S'), conf->count+1); + snprintf(name, 20, "DVB-%c card n. %d", type==TUNER_TER ? 'T' : (type==TUNER_CBL ? 'C' : 'S'), conf->count+1); conf->cards[conf->count].name = name; conf->count++; }
Hello, On Sat, Mar 03, 2007 at 02:10:31PM +0100, nicodvb wrote:
@@ -764,7 +763,7 @@ dvb_config_t *dvb_get_config(void) conf->cards = NULL; for(i=0; i<MAX_CARDS; i++) { - sprintf(filename, "/dev/dvb/adapter%d/frontend0", i); + snprintf(filename, sizeof(filename), "/dev/dvb/adapter%d/frontend0", i);
I simply can't bring myself to see the sense in this from a security standpoint (disregarding that there is just no issue here). For the cases snprintf actually catches, we replace a case that has at least a chance of "just" crashing with a case that is _guaranteed_ to do something unintended leaving the program continue to run and do this unintended thing. If anything, replace this with some other function that aborts if we go beyond the buffer boundaries. snprintf just is not in any way more safe than sprintf, it just has other issues, and is likely to increase complexity - something that always reduces the safety of code. Greetings, Reimar Döffinger
participants (2)
-
nicodvb -
Reimar Döffinger