Skip to content

Commit 7c4345e

Browse files
quic-anankulkEkansh
authored andcommitted
Resolving the buffer accessed out of bound issue
While packing shared buffer with names of all shared objects present in custom DSP_LIBRARY_PATH , their is a possibility of buffer overflow if the shared object names are exceeding the desired limit.The change makes sure the limit is not exceeded thus avoiding buffer overflow. Also, the buffer was allocated with 1KB memory which might fall short to accomodate all the needed shared object names so, increasing this size to 2KB. Signed-off-by: Anand Kulkarni <quic_anankulk@quicinc.com>
1 parent 3512dc8 commit 7c4345e

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

inc/apps_std_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define ADSP_LIBRARY_PATH "ADSP_LIBRARY_PATH"
2020
#define DSP_LIBRARY_PATH "DSP_LIBRARY_PATH"
2121
#define ADSP_AVS_PATH "ADSP_AVS_CFG_PATH"
22+
#define MAX_NON_PRELOAD_LIBS_LEN 2048
23+
#define FILE_EXT ".so"
2224

2325
// Locations where shell file can be found
2426
#ifndef ENABLE_UPSTREAM_DRIVER_INTERFACE

src/fastrpc_procbuf.c

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
/* size of buffer used to share the inital config params to dsp */
2222
#define PROC_SHAREDBUF_SIZE (4*1024)
23-
#define ENV_PATH_LEN 256
2423
#define WORD_SIZE 4
2524

2625
extern struct handle_list *hlist;
@@ -69,13 +68,16 @@ int proc_sharedbuf_init(int dev, int domain) {
6968

7069
static int get_non_preload_lib_names (char** lib_names, size_t* buffer_size, int domain)
7170
{
72-
int nErr = AEE_SUCCESS, env_list_len = 0;
71+
int nErr = AEE_SUCCESS, env_list_len = 0, concat_len = 0, new_len = 0;
7372
char* data_paths = NULL;
7473
char *saveptr = NULL;
75-
VERIFYC(NULL != (data_paths = calloc(1, sizeof(char) * ENV_PATH_LEN)), AEE_ENOMEMORY);
76-
VERIFY(AEE_SUCCESS == (nErr = apps_std_getenv(DSP_LIBRARY_PATH, data_paths, ENV_PATH_LEN, &env_list_len)));
74+
size_t dsp_search_path_len = std_strlen(DSP_LIBRARY_PATH) + 1;
7775

78-
char* path = strtok_r(data_paths, ":", &saveptr);
76+
VERIFYC(*lib_names != NULL, AEE_ENOMEMORY);
77+
VERIFYC(NULL != (data_paths = calloc(1, sizeof(char) * dsp_search_path_len)), AEE_ENOMEMORY);
78+
VERIFYC(AEE_SUCCESS == apps_std_getenv(DSP_LIBRARY_PATH, data_paths, dsp_search_path_len, &env_list_len), AEE_EGETENV);
79+
80+
char* path = strtok_r(data_paths, ";", &saveptr);
7981
while (path != NULL)
8082
{
8183
struct dirent *entry;
@@ -85,23 +87,31 @@ static int get_non_preload_lib_names (char** lib_names, size_t* buffer_size, int
8587
while ((entry = readdir(dir)) != NULL) {
8688
if ( entry -> d_type == DT_REG) {
8789
char* file = entry->d_name;
88-
if ( strstr (file, ".so") != NULL) {
89-
size_t new_len = *buffer_size + std_strlen(file) + 1;
90-
VERIFYC(NULL != (*lib_names = realloc (*lib_names, new_len)), AEE_ENOMEMORY);
91-
std_strlcat(*lib_names, file, new_len);
92-
std_strlcat(*lib_names, ";", new_len);
93-
*buffer_size = new_len;
90+
if (std_strstr(file, FILE_EXT) != NULL) {
91+
if (concat_len + std_strlen(file) > MAX_NON_PRELOAD_LIBS_LEN) {
92+
FARF(ALWAYS,"ERROR: Failed to pack library names in custom DSP_LIBRARY_PATH as required buffer size exceeds Max limit (%d).", MAX_NON_PRELOAD_LIBS_LEN);
93+
nErr = AEE_EBUFFERTOOSMALL;
94+
closedir(dir);
95+
goto bail;
96+
}
97+
std_strlcat(*lib_names, file, MAX_NON_PRELOAD_LIBS_LEN);
98+
concat_len = std_strlcat(*lib_names, ";", MAX_NON_PRELOAD_LIBS_LEN);
9499
}
95100
}
96101
}
97-
closedir(dir);
98-
path = strtok_r(NULL,":", &saveptr);
102+
if (dir != NULL) {
103+
closedir(dir);
104+
}
105+
path = strtok_r(NULL,";", &saveptr);
99106
}
100-
(*lib_names)[*buffer_size - 1] = '\0';
101107
*buffer_size = std_strlen(*lib_names) + 1;
102108

103109
bail:
104-
if (nErr) {
110+
if (data_paths) {
111+
free(data_paths);
112+
data_paths = NULL;
113+
}
114+
if (nErr && (nErr != AEE_EGETENV)) {
105115
FARF(ERROR, "Error 0x%x: %s Failed for domain %d (%s)\n",
106116
nErr, __func__, domain, strerror(errno));
107117
}
@@ -238,14 +248,15 @@ void fastrpc_process_pack_params(int dev, int domain) {
238248
FARF(ERROR, "Error 0x%x: %s: Failed to pack effective domain id %d in shared buffer",
239249
nErr, __func__, domain);
240250
}
241-
if (AEE_SUCCESS != get_non_preload_lib_names(&lib_names, &buffer_size, domain)){
242-
return;
243-
}
244-
nErr = pack_proc_shared_buf_params(domain, CUSTOM_DSP_SEARCH_PATH_LIBS_ID,
245-
lib_names, buffer_size);
246-
if (nErr) {
247-
FARF(ERROR, "Error 0x%x: %s: Failed to pack the directory list in shared buffer",
248-
nErr, __func__);
251+
lib_names = (char *)malloc(sizeof(char) * MAX_NON_PRELOAD_LIBS_LEN);
252+
if (lib_names) {
253+
if (AEE_SUCCESS == get_non_preload_lib_names(&lib_names, &buffer_size, domain)) {
254+
nErr = pack_proc_shared_buf_params(domain, CUSTOM_DSP_SEARCH_PATH_LIBS_ID, lib_names, buffer_size);
255+
if (nErr) {
256+
FARF(ERROR, "Error 0x%x: %s: Failed to pack the directory list in shared buffer",
257+
nErr, __func__);
258+
}
259+
}
249260
}
250261
nErr = pack_proc_shared_buf_params(domain, HLOS_PROC_SESS_ID,
251262
&sess_id, sizeof(sess_id));

0 commit comments

Comments
 (0)