From 30755fef047d3e04bf9c2294ecdeec94fb49c5a2 Mon Sep 17 00:00:00 2001 From: Nicolas Benech Date: Thu, 6 Feb 2020 12:38:50 -0500 Subject: [PATCH] gpu: nvgpu: mm: use constants for string lengths For VM and allocator names, hardcoded constants were used which can be a weakness. This patch uses proper defines in headers instead. JIRA NVGPU-4946 Change-Id: I1cc100a558d0c44c208a7e579cc36b71a0d4eeec Signed-off-by: Nicolas Benech Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2291069 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-cert Reviewed-by: Deepak Nibade Reviewed-by: mobile promotions GVS: Gerrit_Virtual_Submit Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/mm/as.c | 2 +- drivers/gpu/nvgpu/common/mm/vm.c | 10 +++++++++- drivers/gpu/nvgpu/include/nvgpu/allocator.h | 4 +++- drivers/gpu/nvgpu/include/nvgpu/vm.h | 4 +++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/nvgpu/common/mm/as.c b/drivers/gpu/nvgpu/common/mm/as.c index 70891f9b4..3b9a1b498 100644 --- a/drivers/gpu/nvgpu/common/mm/as.c +++ b/drivers/gpu/nvgpu/common/mm/as.c @@ -56,7 +56,7 @@ static int gk20a_vm_alloc_share(struct gk20a_as_share *as_share, struct gk20a *g = gk20a_from_as(as); struct mm_gk20a *mm = &g->mm; struct vm_gk20a *vm; - char name[32]; + char name[NVGPU_VM_NAME_LEN]; char *p; const bool userspace_managed = (flags & NVGPU_AS_ALLOC_USERSPACE_MANAGED) != 0U; diff --git a/drivers/gpu/nvgpu/common/mm/vm.c b/drivers/gpu/nvgpu/common/mm/vm.c index ca0b0a52b..c166add93 100644 --- a/drivers/gpu/nvgpu/common/mm/vm.c +++ b/drivers/gpu/nvgpu/common/mm/vm.c @@ -398,7 +398,15 @@ static int nvgpu_vm_init_user_vma(struct gk20a *g, struct vm_gk20a *vm, const char *name) { int err = 0; - char alloc_name[32]; + char alloc_name[NVGPU_ALLOC_NAME_LEN]; + size_t name_len; + + name_len = strlen("gk20a_") + strlen(name); + if (name_len >= NVGPU_ALLOC_NAME_LEN) { + nvgpu_err(g, "Invalid MAX_NAME_SIZE %lu %u", name_len, + NVGPU_ALLOC_NAME_LEN); + return -EINVAL; + } /* * User VMA. diff --git a/drivers/gpu/nvgpu/include/nvgpu/allocator.h b/drivers/gpu/nvgpu/include/nvgpu/allocator.h index 274616c7a..77629bbb4 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/allocator.h +++ b/drivers/gpu/nvgpu/include/nvgpu/allocator.h @@ -57,6 +57,8 @@ struct nvgpu_alloc_carveout; struct vm_gk20a; struct gk20a; +#define NVGPU_ALLOC_NAME_LEN 32U + /** * Structure containing operations for an allocator to implement. */ @@ -217,7 +219,7 @@ struct nvgpu_allocator { /** * Name of allocator. */ - char name[32]; + char name[NVGPU_ALLOC_NAME_LEN]; /** * Synchronization mutex. */ diff --git a/drivers/gpu/nvgpu/include/nvgpu/vm.h b/drivers/gpu/nvgpu/include/nvgpu/vm.h index 761857af2..f3452d24c 100644 --- a/drivers/gpu/nvgpu/include/nvgpu/vm.h +++ b/drivers/gpu/nvgpu/include/nvgpu/vm.h @@ -97,6 +97,8 @@ struct nvgpu_os_buffer; #include #endif +#define NVGPU_VM_NAME_LEN 20U + /** * This structure describes the properties of batch mapping/unmapping. */ @@ -222,7 +224,7 @@ struct vm_gk20a { */ struct gk20a_as_share *as_share; /** Name of the Virtual Memory context. */ - char name[20]; + char name[NVGPU_VM_NAME_LEN]; /** Start GPU address of the context. */ u64 va_start;