Commit 2b87f3aa authored by Dan Carpenter's avatar Dan Carpenter Committed by John W. Linville

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: default avatarDan Carpenter <error27@gmail.com>
Signed-off-by: default avatarJohn W. Linville <linville@tuxdriver.com>
parent 56824223
...@@ -42,7 +42,7 @@ static ssize_t read_file_debug(struct file *file, char __user *user_buf, ...@@ -42,7 +42,7 @@ static ssize_t read_file_debug(struct file *file, char __user *user_buf,
char buf[32]; char buf[32];
unsigned int len; unsigned int len;
len = snprintf(buf, sizeof(buf), "0x%08x\n", common->debug_mask); len = sprintf(buf, "0x%08x\n", common->debug_mask);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -86,7 +86,7 @@ static ssize_t read_file_tx_chainmask(struct file *file, char __user *user_buf, ...@@ -86,7 +86,7 @@ static ssize_t read_file_tx_chainmask(struct file *file, char __user *user_buf,
char buf[32]; char buf[32];
unsigned int len; unsigned int len;
len = snprintf(buf, sizeof(buf), "0x%08x\n", common->tx_chainmask); len = sprintf(buf, "0x%08x\n", common->tx_chainmask);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -128,7 +128,7 @@ static ssize_t read_file_rx_chainmask(struct file *file, char __user *user_buf, ...@@ -128,7 +128,7 @@ static ssize_t read_file_rx_chainmask(struct file *file, char __user *user_buf,
char buf[32]; char buf[32];
unsigned int len; unsigned int len;
len = snprintf(buf, sizeof(buf), "0x%08x\n", common->rx_chainmask); len = sprintf(buf, "0x%08x\n", common->rx_chainmask);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -248,6 +248,9 @@ static ssize_t read_file_dma(struct file *file, char __user *user_buf, ...@@ -248,6 +248,9 @@ static ssize_t read_file_dma(struct file *file, char __user *user_buf,
ath9k_ps_restore(sc); ath9k_ps_restore(sc);
if (len > DMA_BUF_LEN)
len = DMA_BUF_LEN;
retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf); kfree(buf);
return retval; return retval;
...@@ -363,6 +366,9 @@ static ssize_t read_file_interrupt(struct file *file, char __user *user_buf, ...@@ -363,6 +366,9 @@ static ssize_t read_file_interrupt(struct file *file, char __user *user_buf,
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
"%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total); "%8s: %10u\n", "TOTAL", sc->debug.stats.istats.total);
if (len > sizeof(buf))
len = sizeof(buf);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -402,11 +408,10 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf, ...@@ -402,11 +408,10 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
if (sc->cur_rate_table == NULL) if (sc->cur_rate_table == NULL)
return 0; return 0;
max = 80 + sc->cur_rate_table->rate_cnt * 1024; max = 80 + sc->cur_rate_table->rate_cnt * 1024 + 1;
buf = kmalloc(max + 1, GFP_KERNEL); buf = kmalloc(max, GFP_KERNEL);
if (buf == NULL) if (buf == NULL)
return 0; return 0;
buf[max] = 0;
len += sprintf(buf, "%6s %6s %6s " len += sprintf(buf, "%6s %6s %6s "
"%10s %10s %10s %10s\n", "%10s %10s %10s %10s\n",
...@@ -448,6 +453,9 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf, ...@@ -448,6 +453,9 @@ static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
stats->per); stats->per);
} }
if (len > max)
len = max;
retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf); kfree(buf);
return retval; return retval;
...@@ -510,6 +518,9 @@ static ssize_t read_file_wiphy(struct file *file, char __user *user_buf, ...@@ -510,6 +518,9 @@ static ssize_t read_file_wiphy(struct file *file, char __user *user_buf,
len += snprintf(buf + len, sizeof(buf) - len, len += snprintf(buf + len, sizeof(buf) - len,
"addrmask: %pM\n", addr); "addrmask: %pM\n", addr);
if (len > sizeof(buf))
len = sizeof(buf);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -653,6 +664,9 @@ static ssize_t read_file_xmit(struct file *file, char __user *user_buf, ...@@ -653,6 +664,9 @@ static ssize_t read_file_xmit(struct file *file, char __user *user_buf,
PR("DATA Underrun: ", data_underrun); PR("DATA Underrun: ", data_underrun);
PR("DELIM Underrun: ", delim_underrun); PR("DELIM Underrun: ", delim_underrun);
if (len > size)
len = size;
retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf); kfree(buf);
...@@ -756,6 +770,9 @@ static ssize_t read_file_recv(struct file *file, char __user *user_buf, ...@@ -756,6 +770,9 @@ static ssize_t read_file_recv(struct file *file, char __user *user_buf,
PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL); PHY_ERR("HT-LENGTH", ATH9K_PHYERR_HT_LENGTH_ILLEGAL);
PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL); PHY_ERR("HT-RATE", ATH9K_PHYERR_HT_RATE_ILLEGAL);
if (len > size)
len = size;
retval = simple_read_from_buffer(user_buf, count, ppos, buf, len); retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
kfree(buf); kfree(buf);
...@@ -807,7 +824,7 @@ static ssize_t read_file_regidx(struct file *file, char __user *user_buf, ...@@ -807,7 +824,7 @@ static ssize_t read_file_regidx(struct file *file, char __user *user_buf,
char buf[32]; char buf[32];
unsigned int len; unsigned int len;
len = snprintf(buf, sizeof(buf), "0x%08x\n", sc->debug.regidx); len = sprintf(buf, "0x%08x\n", sc->debug.regidx);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
...@@ -848,7 +865,7 @@ static ssize_t read_file_regval(struct file *file, char __user *user_buf, ...@@ -848,7 +865,7 @@ static ssize_t read_file_regval(struct file *file, char __user *user_buf,
u32 regval; u32 regval;
regval = REG_READ_D(ah, sc->debug.regidx); regval = REG_READ_D(ah, sc->debug.regidx);
len = snprintf(buf, sizeof(buf), "0x%08x\n", regval); len = sprintf(buf, "0x%08x\n", regval);
return simple_read_from_buffer(user_buf, count, ppos, buf, len); return simple_read_from_buffer(user_buf, count, ppos, buf, len);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment