Skip to content

Commit 5ebb2e7

Browse files
authored
Merge pull request #948 from PS2HomeDeveloper/patch-4
iop/smbman: fix buffer overflow in smb_dopen and missing NUL terminators
2 parents 0890eae + fecc501 commit 5ebb2e7

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

‎iop/network/smbman/src/smb_fio.c‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ static char *prepare_path(const char *path, char *full_path, int max_path)
301301

302302
// Reserve space for 2 backslashes and a NULL.
303303
strncpy(full_path, smb_curdir, max_path - 3);
304+
full_path[max_path - 3] = '\0';
304305
strcat(full_path, "\\");
305306

306307
// Skip all leading slashes and backslashes.
@@ -358,7 +359,8 @@ int smb_open(iop_file_t *f, const char *filename, int flags, int mode)
358359
fh->filesize = 0;
359360
else if (fh->mode & O_APPEND)
360361
fh->position = filesize;
361-
strncpy(fh->name, path, SMB_NAME_MAX);
362+
strncpy(fh->name, path, SMB_NAME_MAX - 1);
363+
fh->name[SMB_NAME_MAX - 1] = '\0';
362364
r = 0;
363365
}
364366
} else
@@ -647,6 +649,10 @@ int smb_dopen(iop_file_t *f, const char *dirname)
647649
if (!(info.FileAttributes & EXT_ATTR_DIRECTORY)) {
648650
r = -ENOTDIR;
649651
goto io_unlock;
652+
}// The search pattern (path + optional backslash + '*') must fit in FHANDLE.name.
653+
if (strlen(path) + 3 > SMB_NAME_MAX) {
654+
r = -ENAMETOOLONG;
655+
goto io_unlock;
650656
}
651657

652658
fh = smbman_getfilefreeslot();
@@ -657,7 +663,8 @@ int smb_dopen(iop_file_t *f, const char *dirname)
657663
fh->filesize = 0;
658664
fh->position = 0;
659665

660-
strncpy(fh->name, path, 255);
666+
strncpy(fh->name, path, SMB_NAME_MAX - 1);
667+
fh->name[SMB_NAME_MAX - 1] = '\0';
661668
if (fh->name[strlen(fh->name) - 1] != '\\')
662669
strcat(fh->name, "\\");
663670
strcat(fh->name, "*");
@@ -711,7 +718,7 @@ int smb_dread(iop_file_t *f, iox_dirent_t *dirent)
711718

712719
if (r == 1) {
713720
smb_statFiller(&info->fileInfo, &dirent->stat);
714-
strncpy(dirent->name, info->FileName, SMB_NAME_MAX);
721+
strncpy(dirent->name, info->FileName, sizeof(dirent->name) - 1);
715722
}
716723

717724
io_unlock:

0 commit comments

Comments
 (0)