Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

atm: use scnprintf() instead of sprintf()

As reported by Chen Gang <gang.chen@asianux.com>, we should ensure there
is enough space when formatting the sysfs buffers.

Signed-off-by: Chas Williams <chas@cmf.nrl.navy.mil>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

chas williams - CONTRACTOR and committed by
David S. Miller
3c417771 4e4b5376

+15 -25
+15 -25
net/atm/atm_sysfs.c
··· 14 14 struct device_attribute *attr, char *buf) 15 15 { 16 16 struct atm_dev *adev = to_atm_dev(cdev); 17 - return sprintf(buf, "%s\n", adev->type); 17 + 18 + return scnprintf(buf, PAGE_SIZE, "%s\n", adev->type); 18 19 } 19 20 20 21 static ssize_t show_address(struct device *cdev, 21 22 struct device_attribute *attr, char *buf) 22 23 { 23 - char *pos = buf; 24 24 struct atm_dev *adev = to_atm_dev(cdev); 25 - int i; 26 25 27 - for (i = 0; i < (ESI_LEN - 1); i++) 28 - pos += sprintf(pos, "%02x:", adev->esi[i]); 29 - pos += sprintf(pos, "%02x\n", adev->esi[i]); 30 - 31 - return pos - buf; 26 + return scnprintf(buf, PAGE_SIZE, "%pM\n", adev->esi); 32 27 } 33 28 34 29 static ssize_t show_atmaddress(struct device *cdev, 35 30 struct device_attribute *attr, char *buf) 36 31 { 37 32 unsigned long flags; 38 - char *pos = buf; 39 33 struct atm_dev *adev = to_atm_dev(cdev); 40 34 struct atm_dev_addr *aaddr; 41 35 int bin[] = { 1, 2, 10, 6, 1 }, *fmt = bin; 42 - int i, j; 36 + int i, j, count = 0; 43 37 44 38 spin_lock_irqsave(&adev->lock, flags); 45 39 list_for_each_entry(aaddr, &adev->local, entry) { 46 40 for (i = 0, j = 0; i < ATM_ESA_LEN; ++i, ++j) { 47 41 if (j == *fmt) { 48 - pos += sprintf(pos, "."); 42 + count += scnprintf(buf + count, 43 + PAGE_SIZE - count, "."); 49 44 ++fmt; 50 45 j = 0; 51 46 } 52 - pos += sprintf(pos, "%02x", 53 - aaddr->addr.sas_addr.prv[i]); 47 + count += scnprintf(buf + count, 48 + PAGE_SIZE - count, "%02x", 49 + aaddr->addr.sas_addr.prv[i]); 54 50 } 55 - pos += sprintf(pos, "\n"); 51 + count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 56 52 } 57 53 spin_unlock_irqrestore(&adev->lock, flags); 58 54 59 - return pos - buf; 55 + return count; 60 56 } 61 57 62 58 static ssize_t show_atmindex(struct device *cdev, ··· 60 64 { 61 65 struct atm_dev *adev = to_atm_dev(cdev); 62 66 63 - return sprintf(buf, "%d\n", adev->number); 67 + return scnprintf(buf, PAGE_SIZE, "%d\n", adev->number); 64 68 } 65 69 66 70 static ssize_t show_carrier(struct device *cdev, 67 71 struct device_attribute *attr, char *buf) 68 72 { 69 - char *pos = buf; 70 73 struct atm_dev *adev = to_atm_dev(cdev); 71 74 72 - pos += sprintf(pos, "%d\n", 73 - adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); 74 - 75 - return pos - buf; 75 + return scnprintf(buf, PAGE_SIZE, "%d\n", 76 + adev->signal == ATM_PHY_SIG_LOST ? 0 : 1); 76 77 } 77 78 78 79 static ssize_t show_link_rate(struct device *cdev, 79 80 struct device_attribute *attr, char *buf) 80 81 { 81 - char *pos = buf; 82 82 struct atm_dev *adev = to_atm_dev(cdev); 83 83 int link_rate; 84 84 ··· 92 100 default: 93 101 link_rate = adev->link_rate * 8 * 53; 94 102 } 95 - pos += sprintf(pos, "%d\n", link_rate); 96 - 97 - return pos - buf; 103 + return scnprintf(buf, PAGE_SIZE, "%d\n", link_rate); 98 104 } 99 105 100 106 static DEVICE_ATTR(address, S_IRUGO, show_address, NULL);