aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/wireless/ath/ath9k/debug.c
diff options
context:
space:
mode:
authorDan Carpenter <error27@gmail.com>2010-05-14 09:24:37 -0400
committerJohn W. Linville <linville@tuxdriver.com>2010-06-02 16:13:07 -0400
commit2b87f3aac04818f720956e2b70f9b04fc8e2c794 (patch)
tree9fa916f24bab2b5d98259e76bd17afae76ed2abe /drivers/net/wireless/ath/ath9k/debug.c
parent56824223ac97ca845652c59bed9ce139e100261b (diff)
ath9k/debug: improve the snprintf() handling
The snprintf() function returns the number of bytes that *would* have been written (not counting the NULL terminator) and that can potentally be more than the size of the buffer. In this patch if there were one liners where string clearly fits into the buffer, then I changed snprintf to sprintf(). It's confusing to use the return value of snprintf() as a limitter without verifying that it's smaller than size. This is what initially caught my attention here. If we use the return value of sprintf() instead future code auditors will assume we've verified that it fits already. Also I did find some places where it made sense to use the return value after we've verified that it is smaller than the buffer size. Finally the read_file_rcstat() function added an explicit NULL terminator before calling snprintf(). That's unnecessary because snprintf() will add the null terminator automatically. Signed-off-by: Dan Carpenter <error27@gmail.com> Signed-off-by: John W. Linville <linville@tuxdriver.com>
Diffstat (limited to 'drivers/net/wireless/ath/ath9k/debug.c')
-rw-r--r--drivers/net/wireless/ath/ath9k/debug.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/drivers/net/wireless/ath/ath9k/debug.c b/drivers/net/wireless/ath/ath9k/debug.c
index ee8387740ebe..ad7107164f20 100644
--- a/drivers/net/wireless/ath/ath9k/debug.c
+++ b/drivers/net/wireless/ath/ath9k/debug.c
@@ -42,7 +42,7 @@ static ssize_t read_file_debug(struct file *file, char __user *user_buf,
42 char buf[32]; 42 char buf[32];
43 unsigned int len; 43 unsigned int len;
44 44
45 len = snprintf(buf, sizeof(buf), "0x%08x\n", common->debug_mask); 45 len = sprintf(buf, "0x%08x\n", common->debug_mask);
46 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 46 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
47} 47}
48 48
@@ -86,7 +86,7 @@ static ssize_t read_file_tx_chainmask(struct file *file, char __user *user_buf,
86 char buf[32]; 86 char buf[32];
87 unsigned int len; 87 unsigned int len;
88 88
89 len = snprintf(buf, sizeof(buf), "0x%08x\n", common->tx_chainmask); 89 len = sprintf(buf, "0x%08x\n", common->tx_chainmask);
90 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 90 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
91} 91}
92 92
@@ -128,7 +128,7 @@ static ssize_t read_file_rx_chainmask(struct file *file, char __user *user_buf,
128 char buf[32]; 128 char buf[32];
129 unsigned int len; 129 unsigned int len;
130 130
131 len = snprintf(buf, sizeof(buf), "0x%08x\n", common->rx_chainmask); 131 len = sprintf(buf, "0x%08x\n", common->rx_chainmask);
132 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 132 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
133} 133}
134 134
@@ -248,6 +248,9 @@ static ssize_t read_file_dma(struct file *file, char __user *user_buf,
248 248
249 ath9k_ps_restore(sc); 249 ath9k_ps_restore(sc);
250 250
251 if (len > DMA_BUF_LEN)
252 len = DMA_BUF_LEN;
253
251 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); 254 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
252 kfree(buf); 255 kfree(buf);
253 return retval; 256 return retval;
@@ -363,6 +366,9 @@ static ssize_t read_file_interrupt(struct file *file, char __user *user_buf,
363 len += snprintf(buf + len, sizeof(buf) - len, 366 len += snprintf(buf + len, sizeof(buf) - len,
364 "%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total); 367 "%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total);
365 368
369 if (len > sizeof(buf))
370 len = sizeof(buf);
371
366 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 372 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
367} 373}
368 374
@@ -402,11 +408,10 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
402 if (sc->cur_rate_table == NULL) 408 if (sc->cur_rate_table == NULL)
403 return 0; 409 return 0;
404 410
405 max = 80 + sc->cur_rate_table->rate_cnt * 1024; 411 max = 80 + sc->cur_rate_table->rate_cnt * 1024 + 1;
406 buf = kmalloc(max + 1, GFP_KERNEL); 412 buf = kmalloc(max, GFP_KERNEL);
407 if (buf == NULL) 413 if (buf == NULL)
408 return 0; 414 return 0;
409 buf[max] = 0;
410 415
411 len += sprintf(buf, "%6s %6s %6s " 416 len += sprintf(buf, "%6s %6s %6s "
412 "%10s %10s %10s %10s\n", 417 "%10s %10s %10s %10s\n",
@@ -448,6 +453,9 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
448 stats->per); 453 stats->per);
449 } 454 }
450 455
456 if (len > max)
457 len = max;
458
451 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); 459 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
452 kfree(buf); 460 kfree(buf);
453 return retval; 461 return retval;
@@ -510,6 +518,9 @@ static ssize_t read_file_wiphy(struct file *file, char __user *user_buf,
510 len += snprintf(buf + len, sizeof(buf) - len, 518 len += snprintf(buf + len, sizeof(buf) - len,
511 "addrmask: %pM\n", addr); 519 "addrmask: %pM\n", addr);
512 520
521 if (len > sizeof(buf))
522 len = sizeof(buf);
523
513 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 524 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
514} 525}
515 526
@@ -653,6 +664,9 @@ static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
653 PR("DATA Underrun: ", data_underrun); 664 PR("DATA Underrun: ", data_underrun);
654 PR("DELIM Underrun: ", delim_underrun); 665 PR("DELIM Underrun: ", delim_underrun);
655 666
667 if (len > size)
668 len = size;
669
656 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); 670 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
657 kfree(buf); 671 kfree(buf);
658 672
@@ -756,6 +770,9 @@ static ssize_t read_file_recv(struct file *file, char __user *user_buf,
756 PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL); 770 PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL);
757 PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL); 771 PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL);
758 772
773 if (len > size)
774 len = size;
775
759 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); 776 retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
760 kfree(buf); 777 kfree(buf);
761 778
@@ -807,7 +824,7 @@ static ssize_t read_file_regidx(struct file *file, char __user *user_buf,
807 char buf[32]; 824 char buf[32];
808 unsigned int len; 825 unsigned int len;
809 826
810 len = snprintf(buf, sizeof(buf), "0x%08x\n", sc->debug.regidx); 827 len = sprintf(buf, "0x%08x\n", sc->debug.regidx);
811 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 828 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
812} 829}
813 830
@@ -848,7 +865,7 @@ static ssize_t read_file_regval(struct file *file, char __user *user_buf,
848 u32 regval; 865 u32 regval;
849 866
850 regval = REG_READ_D(ah, sc->debug.regidx); 867 regval = REG_READ_D(ah, sc->debug.regidx);
851 len = snprintf(buf, sizeof(buf), "0x%08x\n", regval); 868 len = sprintf(buf, "0x%08x\n", regval);
852 return simple_read_from_buffer(user_buf, count, ppos, buf, len); 869 return simple_read_from_buffer(user_buf, count, ppos, buf, len);
853} 870}
854 871