Skip to content

Commit a1f1c24

Browse files
JustinStittVudentz
authored andcommitted
Bluetooth: replace deprecated strncpy with strscpy_pad
strncpy() is deprecated for use on NUL-terminated destination strings [0] and as such we should prefer more robust and less ambiguous string interfaces. The CAPI (part II) [1] states that the manufacturer id should be a "zero-terminated ASCII string" and should "always [be] zero-terminated." Much the same for the serial number: "The serial number, a seven-digit number coded as a zero-terminated ASCII string". Along with this, its clear the original author intended for these buffers to be NUL-padded as well. To meet the specification as well as properly NUL-pad, use strscpy_pad(). In doing this, an opportunity to simplify this code is also present. Remove the min_t() and combine the length check into the main if. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [0] Link: https://capi.org/downloads.html [1] Link: https://manpages.debian.org/testing/linux-manual-4.8/strscpy.9.en.html Link: KSPP#90 Cc: [email protected] Signed-off-by: Justin Stitt <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent d47da6b commit a1f1c24

File tree

1 file changed

+8
-24
lines changed

1 file changed

+8
-24
lines changed

net/bluetooth/cmtp/capi.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,10 @@ static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *s
248248
break;
249249

250250
case CAPI_FUNCTION_GET_MANUFACTURER:
251-
if (skb->len < CAPI_MSG_BASELEN + 15)
252-
break;
253-
254-
if (!info && ctrl) {
255-
int len = min_t(uint, CAPI_MANUFACTURER_LEN,
256-
skb->data[CAPI_MSG_BASELEN + 14]);
257-
258-
memset(ctrl->manu, 0, CAPI_MANUFACTURER_LEN);
259-
strncpy(ctrl->manu,
260-
skb->data + CAPI_MSG_BASELEN + 15, len);
261-
}
262-
251+
if (!info && ctrl && skb->len > CAPI_MSG_BASELEN + 14)
252+
strscpy_pad(ctrl->manu,
253+
skb->data + CAPI_MSG_BASELEN + 15,
254+
skb->data[CAPI_MSG_BASELEN + 14]);
263255
break;
264256

265257
case CAPI_FUNCTION_GET_VERSION:
@@ -276,18 +268,10 @@ static void cmtp_recv_interopmsg(struct cmtp_session *session, struct sk_buff *s
276268
break;
277269

278270
case CAPI_FUNCTION_GET_SERIAL_NUMBER:
279-
if (skb->len < CAPI_MSG_BASELEN + 17)
280-
break;
281-
282-
if (!info && ctrl) {
283-
int len = min_t(uint, CAPI_SERIAL_LEN,
284-
skb->data[CAPI_MSG_BASELEN + 16]);
285-
286-
memset(ctrl->serial, 0, CAPI_SERIAL_LEN);
287-
strncpy(ctrl->serial,
288-
skb->data + CAPI_MSG_BASELEN + 17, len);
289-
}
290-
271+
if (!info && ctrl && skb->len > CAPI_MSG_BASELEN + 16)
272+
strscpy_pad(ctrl->serial,
273+
skb->data + CAPI_MSG_BASELEN + 17,
274+
skb->data[CAPI_MSG_BASELEN + 16]);
291275
break;
292276
}
293277

0 commit comments

Comments
 (0)