Skip to content

Commit ab11dac

Browse files
tuhaowengregkh
authored andcommitted
dev/parport: fix the array out-of-bounds risk
Fixed array out-of-bounds issues caused by sprintf by replacing it with snprintf for safer data copying, ensuring the destination buffer is not overflowed. Below is the stack trace I encountered during the actual issue: [ 66.575408s] [pid:5118,cpu4,QThread,4]Kernel panic - not syncing: stack-protector: Kernel stack is corrupted in: do_hardware_base_addr+0xcc/0xd0 [parport] [ 66.575408s] [pid:5118,cpu4,QThread,5]CPU: 4 PID: 5118 Comm: QThread Tainted: G S W O 5.10.97-arm64-desktop #7100.57021.2 [ 66.575439s] [pid:5118,cpu4,QThread,6]TGID: 5087 Comm: EFileApp [ 66.575439s] [pid:5118,cpu4,QThread,7]Hardware name: HUAWEI HUAWEI QingYun PGUX-W515x-B081/SP1PANGUXM, BIOS 1.00.07 04/29/2024 [ 66.575439s] [pid:5118,cpu4,QThread,8]Call trace: [ 66.575469s] [pid:5118,cpu4,QThread,9] dump_backtrace+0x0/0x1c0 [ 66.575469s] [pid:5118,cpu4,QThread,0] show_stack+0x14/0x20 [ 66.575469s] [pid:5118,cpu4,QThread,1] dump_stack+0xd4/0x10c [ 66.575500s] [pid:5118,cpu4,QThread,2] panic+0x1d8/0x3bc [ 66.575500s] [pid:5118,cpu4,QThread,3] __stack_chk_fail+0x2c/0x38 [ 66.575500s] [pid:5118,cpu4,QThread,4] do_hardware_base_addr+0xcc/0xd0 [parport] Signed-off-by: tuhaowen <[email protected]> Cc: stable <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent feb1f0c commit ab11dac

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

drivers/parport/procfs.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ static int do_active_device(struct ctl_table *table, int write,
5151

5252
for (dev = port->devices; dev ; dev = dev->next) {
5353
if(dev == port->cad) {
54-
len += sprintf(buffer, "%s\n", dev->name);
54+
len += snprintf(buffer, sizeof(buffer), "%s\n", dev->name);
5555
}
5656
}
5757

5858
if(!len) {
59-
len += sprintf(buffer, "%s\n", "none");
59+
len += snprintf(buffer, sizeof(buffer), "%s\n", "none");
6060
}
6161

6262
if (len > *lenp)
@@ -87,19 +87,19 @@ static int do_autoprobe(struct ctl_table *table, int write,
8787
}
8888

8989
if ((str = info->class_name) != NULL)
90-
len += sprintf (buffer + len, "CLASS:%s;\n", str);
90+
len += snprintf (buffer + len, sizeof(buffer) - len, "CLASS:%s;\n", str);
9191

9292
if ((str = info->model) != NULL)
93-
len += sprintf (buffer + len, "MODEL:%s;\n", str);
93+
len += snprintf (buffer + len, sizeof(buffer) - len, "MODEL:%s;\n", str);
9494

9595
if ((str = info->mfr) != NULL)
96-
len += sprintf (buffer + len, "MANUFACTURER:%s;\n", str);
96+
len += snprintf (buffer + len, sizeof(buffer) - len, "MANUFACTURER:%s;\n", str);
9797

9898
if ((str = info->description) != NULL)
99-
len += sprintf (buffer + len, "DESCRIPTION:%s;\n", str);
99+
len += snprintf (buffer + len, sizeof(buffer) - len, "DESCRIPTION:%s;\n", str);
100100

101101
if ((str = info->cmdset) != NULL)
102-
len += sprintf (buffer + len, "COMMAND SET:%s;\n", str);
102+
len += snprintf (buffer + len, sizeof(buffer) - len, "COMMAND SET:%s;\n", str);
103103

104104
if (len > *lenp)
105105
len = *lenp;
@@ -117,7 +117,7 @@ static int do_hardware_base_addr(struct ctl_table *table, int write,
117117
void *result, size_t *lenp, loff_t *ppos)
118118
{
119119
struct parport *port = (struct parport *)table->extra1;
120-
char buffer[20];
120+
char buffer[64];
121121
int len = 0;
122122

123123
if (*ppos) {
@@ -128,7 +128,7 @@ static int do_hardware_base_addr(struct ctl_table *table, int write,
128128
if (write) /* permissions prevent this anyway */
129129
return -EACCES;
130130

131-
len += sprintf (buffer, "%lu\t%lu\n", port->base, port->base_hi);
131+
len += snprintf (buffer, sizeof(buffer), "%lu\t%lu\n", port->base, port->base_hi);
132132

133133
if (len > *lenp)
134134
len = *lenp;
@@ -155,7 +155,7 @@ static int do_hardware_irq(struct ctl_table *table, int write,
155155
if (write) /* permissions prevent this anyway */
156156
return -EACCES;
157157

158-
len += sprintf (buffer, "%d\n", port->irq);
158+
len += snprintf (buffer, sizeof(buffer), "%d\n", port->irq);
159159

160160
if (len > *lenp)
161161
len = *lenp;
@@ -182,7 +182,7 @@ static int do_hardware_dma(struct ctl_table *table, int write,
182182
if (write) /* permissions prevent this anyway */
183183
return -EACCES;
184184

185-
len += sprintf (buffer, "%d\n", port->dma);
185+
len += snprintf (buffer, sizeof(buffer), "%d\n", port->dma);
186186

187187
if (len > *lenp)
188188
len = *lenp;
@@ -213,7 +213,7 @@ static int do_hardware_modes(struct ctl_table *table, int write,
213213
#define printmode(x) \
214214
do { \
215215
if (port->modes & PARPORT_MODE_##x) \
216-
len += sprintf(buffer + len, "%s%s", f++ ? "," : "", #x); \
216+
len += snprintf(buffer + len, sizeof(buffer) - len, "%s%s", f++ ? "," : "", #x); \
217217
} while (0)
218218
int f = 0;
219219
printmode(PCSPP);

0 commit comments

Comments
 (0)