Skip to content

Commit f505a45

Browse files
tobluxsmfrench
authored andcommitted
smb: client: Use snprintf in cifs_set_cifscreds
Replace unbounded sprintf() calls with the safer snprintf(). Avoid using magic numbers and use strlen() to calculate the key descriptor buffer size. Save the size in a local variable and reuse it for the bounded snprintf() calls. Remove CIFSCREDS_DESC_SIZE. Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev> Acked-by: Paulo Alcantara (Red Hat) <pc@manguebit.org> Signed-off-by: Steve French <stfrench@microsoft.com>
1 parent 2f37dc4 commit f505a45

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

fs/smb/client/connect.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2167,16 +2167,14 @@ void __cifs_put_smb_ses(struct cifs_ses *ses)
21672167

21682168
#ifdef CONFIG_KEYS
21692169

2170-
/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2171-
#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
2172-
21732170
/* Populate username and pw fields from keyring if possible */
21742171
static int
21752172
cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
21762173
{
21772174
int rc = 0;
21782175
int is_domain = 0;
21792176
const char *delim, *payload;
2177+
size_t desc_sz;
21802178
char *desc;
21812179
ssize_t len;
21822180
struct key *key;
@@ -2185,19 +2183,21 @@ cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
21852183
struct sockaddr_in6 *sa6;
21862184
const struct user_key_payload *upayload;
21872185

2188-
desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2186+
/* "cifs:a:" and "cifs:d:" are the same length; +1 for NUL terminator */
2187+
desc_sz = strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1;
2188+
desc = kmalloc(desc_sz, GFP_KERNEL);
21892189
if (!desc)
21902190
return -ENOMEM;
21912191

21922192
/* try to find an address key first */
21932193
switch (server->dstaddr.ss_family) {
21942194
case AF_INET:
21952195
sa = (struct sockaddr_in *)&server->dstaddr;
2196-
sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2196+
snprintf(desc, desc_sz, "cifs:a:%pI4", &sa->sin_addr.s_addr);
21972197
break;
21982198
case AF_INET6:
21992199
sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2200-
sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2200+
snprintf(desc, desc_sz, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
22012201
break;
22022202
default:
22032203
cifs_dbg(FYI, "Bad ss_family (%hu)\n",
@@ -2216,7 +2216,7 @@ cifs_set_cifscreds(struct smb3_fs_context *ctx, struct cifs_ses *ses)
22162216
}
22172217

22182218
/* didn't work, try to find a domain key */
2219-
sprintf(desc, "cifs:d:%s", ses->domainName);
2219+
snprintf(desc, desc_sz, "cifs:d:%s", ses->domainName);
22202220
cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
22212221
key = request_key(&key_type_logon, desc, "");
22222222
if (IS_ERR(key)) {

0 commit comments

Comments
 (0)