From e10f201602549e60069f6c5aab80f0d3c44b4a2c Mon Sep 17 00:00:00 2001 From: ajesh Date: Mon, 8 Mar 2021 10:22:27 +0300 Subject: [PATCH] gpu: nvgpu: add checks as part of BVEC analysis Add checks in common.utils unit as part of BVEC analysis. The check in enabled.c makes sure that unauthorized memory access is not performed and string.c is modified with a check to avoid a possible invocation of BUG. Jira NVGPU-6268 Change-Id: I672c9c54a2d7b61219dee1b249b9e1345381a965 Signed-off-by: ajesh Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2494951 (cherry picked from commit 464e101b23b0143ff2e26e07659e34d1678dbf9d) Reviewed-on: https://git-master.nvidia.com/r/c/linux-nvgpu/+/2497647 Reviewed-by: svc-mobile-coverity Reviewed-by: svc-mobile-cert Reviewed-by: Alex Waterman Reviewed-by: Ankur Kishore Reviewed-by: mobile promotions GVS: Gerrit_Virtual_Submit Tested-by: mobile promotions --- drivers/gpu/nvgpu/common/utils/enabled.c | 12 ++++++++++-- drivers/gpu/nvgpu/common/utils/string.c | 8 ++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/nvgpu/common/utils/enabled.c b/drivers/gpu/nvgpu/common/utils/enabled.c index eb31f4819..aa3f2d22b 100644 --- a/drivers/gpu/nvgpu/common/utils/enabled.c +++ b/drivers/gpu/nvgpu/common/utils/enabled.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2017-2021, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -89,11 +89,19 @@ void nvgpu_free_enabled_flags(struct gk20a *g) bool nvgpu_is_enabled(struct gk20a *g, u32 flag) { - return nvgpu_test_bit(flag, g->enabled_flags); + if (flag < NVGPU_MAX_ENABLED_BITS) { + return nvgpu_test_bit(flag, g->enabled_flags); + } else { + return 0; + } } void nvgpu_set_enabled(struct gk20a *g, u32 flag, bool state) { + if (flag >= NVGPU_MAX_ENABLED_BITS) { + return; + } + if (state) { nvgpu_set_bit(flag, g->enabled_flags); } else { diff --git a/drivers/gpu/nvgpu/common/utils/string.c b/drivers/gpu/nvgpu/common/utils/string.c index a0687e097..2cb50c691 100644 --- a/drivers/gpu/nvgpu/common/utils/string.c +++ b/drivers/gpu/nvgpu/common/utils/string.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, NVIDIA CORPORATION. All rights reserved. + * Copyright (c) 2018-2021, NVIDIA CORPORATION. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -47,6 +47,10 @@ int nvgpu_strnadd_u32(char *dst, const u32 value, size_t size, u32 radix) return 0; } + if (size > ((u64)(INT_MAX))) { + return 0; + } + /* how many digits do we need ? */ n = 0; v = value; @@ -56,7 +60,7 @@ int nvgpu_strnadd_u32(char *dst, const u32 value, size_t size, u32 radix) } while (v > 0U); /* bail out if there is not room for '\0' */ - if (n >= nvgpu_safe_cast_u64_to_s32(size)) { + if (n >= (s32)size) { return 0; }