From d2f579ba8bfd028668d676cc061ae3e0f3f1274a Mon Sep 17 00:00:00 2001 From: Felix Abecassis Date: Fri, 21 Jun 2019 01:25:08 -0700 Subject: [PATCH] Fix out-of-bounds read in ncclStrToCpuset (#233) The affinityStr string was not null-terminated but was passed to strlen(3). Signed-off-by: Felix Abecassis [ROCm/rccl commit: 37e4f8729e5e6604ab739b2353064139af43fe2d] --- projects/rccl/src/init.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/projects/rccl/src/init.cc b/projects/rccl/src/init.cc index 80af287012..66a4865c8f 100644 --- a/projects/rccl/src/init.cc +++ b/projects/rccl/src/init.cc @@ -879,10 +879,12 @@ static ncclResult_t getCpuGpuAffinity(int cudaDev, cpu_set_t* mask) { path[PATH_MAX-1] = '\0'; int fd; SYSCHECKVAL(open(path, O_RDONLY), "open", fd); - char affinityStr[sizeof(cpu_set_t)*2]; + char affinityStr[sizeof(cpu_set_t)*2 + 1]; int r = read(fd, affinityStr, sizeof(cpu_set_t)*2); - if (r > 0) + if (r > 0) { + affinityStr[r] = '\0'; NCCLCHECK(ncclStrToCpuset(affinityStr, mask)); + } close(fd); free(cudaPath); return ncclSuccess;