Commit 1119e98d authored by Silvio Cesare's avatar Silvio Cesare Committed by Greg Kroah-Hartman

scsi: lpfc: change snprintf to scnprintf for possible overflow

commit e7f7b6f3 upstream.

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: default avatarSilvio Cesare <silvio.cesare@gmail.com>
Signed-off-by: default avatarWilly Tarreau <w@1wt.eu>
Signed-off-by: default avatarJames Smart <james.smart@broadcom.com>
Cc: Dick Kennedy <dick.kennedy@broadcom.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: default avatarMartin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 4fb84183
This diff is collapsed.
...@@ -1430,7 +1430,7 @@ lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol, ...@@ -1430,7 +1430,7 @@ lpfc_vport_symbolic_port_name(struct lpfc_vport *vport, char *symbol,
* Name object. NPIV is not in play so this integer * Name object. NPIV is not in play so this integer
* value is sufficient and unique per FC-ID. * value is sufficient and unique per FC-ID.
*/ */
n = snprintf(symbol, size, "%d", vport->phba->brd_no); n = scnprintf(symbol, size, "%d", vport->phba->brd_no);
return n; return n;
} }
...@@ -1444,26 +1444,26 @@ lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol, ...@@ -1444,26 +1444,26 @@ lpfc_vport_symbolic_node_name(struct lpfc_vport *vport, char *symbol,
lpfc_decode_firmware_rev(vport->phba, fwrev, 0); lpfc_decode_firmware_rev(vport->phba, fwrev, 0);
n = snprintf(symbol, size, "Emulex %s", vport->phba->ModelName); n = scnprintf(symbol, size, "Emulex %s", vport->phba->ModelName);
if (size < n) if (size < n)
return n; return n;
n += snprintf(symbol + n, size - n, " FV%s", fwrev); n += scnprintf(symbol + n, size - n, " FV%s", fwrev);
if (size < n) if (size < n)
return n; return n;
n += snprintf(symbol + n, size - n, " DV%s.", n += scnprintf(symbol + n, size - n, " DV%s.",
lpfc_release_version); lpfc_release_version);
if (size < n) if (size < n)
return n; return n;
n += snprintf(symbol + n, size - n, " HN:%s.", n += scnprintf(symbol + n, size - n, " HN:%s.",
init_utsname()->nodename); init_utsname()->nodename);
if (size < n) if (size < n)
return n; return n;
/* Note :- OS name is "Linux" */ /* Note :- OS name is "Linux" */
n += snprintf(symbol + n, size - n, " OS:%s\n", n += scnprintf(symbol + n, size - n, " OS:%s\n",
init_utsname()->sysname); init_utsname()->sysname);
return n; return n;
} }
......
This diff is collapsed.
...@@ -348,7 +348,7 @@ lpfc_debug_dump_qe(struct lpfc_queue *q, uint32_t idx) ...@@ -348,7 +348,7 @@ lpfc_debug_dump_qe(struct lpfc_queue *q, uint32_t idx)
pword = q->qe[idx].address; pword = q->qe[idx].address;
len = 0; len = 0;
len += snprintf(line_buf+len, LPFC_LBUF_SZ-len, "QE[%04d]: ", idx); len += scnprintf(line_buf+len, LPFC_LBUF_SZ-len, "QE[%04d]: ", idx);
if (qe_word_cnt > 8) if (qe_word_cnt > 8)
printk(KERN_ERR "%s\n", line_buf); printk(KERN_ERR "%s\n", line_buf);
...@@ -359,11 +359,11 @@ lpfc_debug_dump_qe(struct lpfc_queue *q, uint32_t idx) ...@@ -359,11 +359,11 @@ lpfc_debug_dump_qe(struct lpfc_queue *q, uint32_t idx)
if (qe_word_cnt > 8) { if (qe_word_cnt > 8) {
len = 0; len = 0;
memset(line_buf, 0, LPFC_LBUF_SZ); memset(line_buf, 0, LPFC_LBUF_SZ);
len += snprintf(line_buf+len, LPFC_LBUF_SZ-len, len += scnprintf(line_buf+len, LPFC_LBUF_SZ-len,
"%03d: ", i); "%03d: ", i);
} }
} }
len += snprintf(line_buf+len, LPFC_LBUF_SZ-len, "%08x ", len += scnprintf(line_buf+len, LPFC_LBUF_SZ-len, "%08x ",
((uint32_t)*pword) & 0xffffffff); ((uint32_t)*pword) & 0xffffffff);
pword++; pword++;
} }
......
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