Skip to content

Commit c407cd0

Browse files
silviocesarebroonie
authored andcommitted
ASoC: imx-audmux: change snprintf to scnprintf for possible overflow
Change snprintf to scnprintf. There are generally two cases where using snprintf causes problems. 1) Uses of size += snprintf(buf, SIZE - size, fmt, ...) In this case, if snprintf would have written more characters than what the buffer size (SIZE) is, then size will end up larger than SIZE. In later uses of snprintf, SIZE - size will result in a negative number, leading to problems. Note that size might already be too large by using size = snprintf before the code reaches a case of size += snprintf. 2) If size is ultimately used as a length parameter for a copy back to user space, then it will potentially allow for a buffer overflow and information disclosure when size is greater than SIZE. When the size is used to index the buffer directly, we can have memory corruption. This also means when size = snprintf... is used, it may also cause problems since size may become large. Copying to userspace is mitigated by the HARDENED_USERCOPY kernel configuration. The solution to these issues is to use scnprintf which returns the number of characters actually written to the buffer, so the size variable will never exceed SIZE. Signed-off-by: Silvio Cesare <silvio.cesare@gmail.com> Cc: Timur Tabi <timur@kernel.org> Cc: Nicolin Chen <nicoleotsuka@gmail.com> Cc: Mark Brown <broonie@kernel.org> Cc: Xiubo Li <Xiubo.Lee@gmail.com> Cc: Fabio Estevam <fabio.estevam@nxp.com> Cc: Dan Carpenter <dan.carpenter@oracle.com> Cc: Kees Cook <keescook@chromium.org> Cc: Will Deacon <will.deacon@arm.com> Cc: Greg KH <greg@kroah.com> Signed-off-by: Willy Tarreau <w@1wt.eu> Acked-by: Nicolin Chen <nicoleotsuka@gmail.com> Reviewed-by: Kees Cook <keescook@chromium.org> Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 060d0bf commit c407cd0

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

sound/soc/fsl/imx-audmux.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,49 +86,49 @@ static ssize_t audmux_read_file(struct file *file, char __user *user_buf,
8686
if (!buf)
8787
return -ENOMEM;
8888

89-
ret = snprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
89+
ret = scnprintf(buf, PAGE_SIZE, "PDCR: %08x\nPTCR: %08x\n",
9090
pdcr, ptcr);
9191

9292
if (ptcr & IMX_AUDMUX_V2_PTCR_TFSDIR)
93-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
93+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
9494
"TxFS output from %s, ",
9595
audmux_port_string((ptcr >> 27) & 0x7));
9696
else
97-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
97+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
9898
"TxFS input, ");
9999

100100
if (ptcr & IMX_AUDMUX_V2_PTCR_TCLKDIR)
101-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
101+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
102102
"TxClk output from %s",
103103
audmux_port_string((ptcr >> 22) & 0x7));
104104
else
105-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
105+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
106106
"TxClk input");
107107

108-
ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
108+
ret += scnprintf(buf + ret, PAGE_SIZE - ret, "\n");
109109

110110
if (ptcr & IMX_AUDMUX_V2_PTCR_SYN) {
111-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
111+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
112112
"Port is symmetric");
113113
} else {
114114
if (ptcr & IMX_AUDMUX_V2_PTCR_RFSDIR)
115-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
115+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
116116
"RxFS output from %s, ",
117117
audmux_port_string((ptcr >> 17) & 0x7));
118118
else
119-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
119+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
120120
"RxFS input, ");
121121

122122
if (ptcr & IMX_AUDMUX_V2_PTCR_RCLKDIR)
123-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
123+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
124124
"RxClk output from %s",
125125
audmux_port_string((ptcr >> 12) & 0x7));
126126
else
127-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
127+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
128128
"RxClk input");
129129
}
130130

131-
ret += snprintf(buf + ret, PAGE_SIZE - ret,
131+
ret += scnprintf(buf + ret, PAGE_SIZE - ret,
132132
"\nData received from %s\n",
133133
audmux_port_string((pdcr >> 13) & 0x7));
134134

0 commit comments

Comments
 (0)