Feature/fileissue#191
Conversation
…d read_suffix_from_file_to_buf Agent-Logs-Url: https://github.com/rdkcentral/remote_debugger/sessions/60c9ea5b-f5bf-4445-9dee-3bfde1cc11f2 Co-authored-by: Abhinavpv28 <162570454+Abhinavpv28@users.noreply.github.com>
Agent-Logs-Url: https://github.com/rdkcentral/remote_debugger/sessions/60c9ea5b-f5bf-4445-9dee-3bfde1cc11f2 Co-authored-by: Abhinavpv28 <162570454+Abhinavpv28@users.noreply.github.com>
Co-authored-by: Abhinavpv28 <162570454+Abhinavpv28@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Code Coverage Summary |
| /* Only persist suffix if input contains an underscore (i.e., is not just the base name) */ | ||
| if (strchr(cmdMap[index], '_') && local_suffix[0] != '\0') | ||
| { | ||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Persisting suffix: '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | ||
| if (persist_suffix_to_file(local_suffix) != 0) | ||
| { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Failed to persist suffix '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Not persisting suffix for input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, cmdMap[index], index); | ||
| } |
Code Coverage Summary |
| } | ||
| return -1; | ||
| } | ||
| ssize_t written = write(tmpfd, suffix + total_written, (ssize_t)to_write); |
There was a problem hiding this comment.
Coverity Issue - Overflowed integer argument
"(ssize_t)to_write", which might have underflowed, is passed to "write(tmpfd, suffix + total_written, (ssize_t)to_write)".
High Impact, CWE-190
INTEGER_OVERFLOW
| dataMsgLen = strlen(cmdMap[index]) + 1; | ||
| char base[BUF_LEN_128] = {0}; | ||
| char local_suffix[BUF_LEN_128] = {0}; | ||
| split_issue_type(cmdMap[index], base, sizeof(base), local_suffix, sizeof(local_suffix)); |
| } | ||
| else | ||
| { | ||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Not persisting suffix for input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, cmdMap[index], index); |
| } | ||
| return -1; | ||
| } | ||
| ssize_t written = write(tmpfd, suffix + total_written, (ssize_t)to_write); |
There was a problem hiding this comment.
Coverity Issue - Overflowed integer argument
"(ssize_t)to_write", which might have underflowed, is passed to "write(tmpfd, suffix + total_written, (ssize_t)to_write)".
High Impact, CWE-190
INTEGER_OVERFLOW
Code Coverage Summary |
| @@ -46,6 +52,225 @@ void removeSpecialChar(char *str) | |||
| } | |||
| } | |||
|
|
|||
| int persist_suffix_to_file(const char *suffix) { | |||
| struct stat st; | |||
| uid_t uid = getuid(); | |||
| int dirfd = open(RRD_SUFFIX_DIR, O_NOFOLLOW | O_DIRECTORY); | |||
| if (dirfd == -1) { | |||
| // Directory does not exist, try to create | |||
| if (mkdir(RRD_SUFFIX_DIR, 0700) != 0 && errno != EEXIST) { | |||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Failed to create %s: %s\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR, strerror(errno)); | |||
| return -1; | |||
| } | |||
| dirfd = open(RRD_SUFFIX_DIR, O_NOFOLLOW | O_DIRECTORY); | |||
| if (dirfd == -1) { | |||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Could not open %s after creation: %s\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR, strerror(errno)); | |||
| return -1; | |||
| } | |||
| } | |||
| if (fstat(dirfd, &st) != 0 || !S_ISDIR(st.st_mode) || st.st_uid != uid || (st.st_mode & 0777) != 0700) { | |||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] %s not owned by current user or not mode 0700!\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR); | |||
| close(dirfd); | |||
| return -1; | |||
| } | |||
| size_t to_write = len - total_written; | ||
| if (to_write > SSIZE_MAX) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] to_write (%zu) > SSIZE_MAX (%zd), refusing to write\n", __FUNCTION__, __LINE__, to_write, (ssize_t)SSIZE_MAX); | ||
| close(tmpfd); |
| @@ -88,9 +91,23 @@ void processIssueTypeEvent(data_buf *rbuf) | |||
| } | |||
| cmdBuff->appendMode = rbuf->appendMode; | |||
| cmdBuff->mdata = (char *)calloc(1, dataMsgLen); | |||
| /* Suffix is now persisted via file, no struct field needed */ | |||
| if (cmdBuff->mdata) | |||
| { | |||
| strncpy((char *)cmdBuff->mdata, cmdMap[index], dataMsgLen); | |||
| strncpy((char *)cmdBuff->mdata, base, dataMsgLen); | |||
| /* Only persist suffix if input contains an underscore (i.e., is not just the base name) */ | |||
| if (strchr(cmdMap[index], '_') && local_suffix[0] != '\0') | |||
| { | |||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Persisting suffix: '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | |||
| if (persist_suffix_to_file(local_suffix) != 0) | |||
| { | |||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Failed to persist suffix '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Not persisting suffix for input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, cmdMap[index], index); | |||
| } | |||
Code Coverage Summary |
| int persist_suffix_to_file(const char *suffix) { | ||
| struct stat st; | ||
| uid_t uid = getuid(); | ||
| int dirfd = open(RRD_SUFFIX_DIR, O_NOFOLLOW | O_DIRECTORY); | ||
| if (dirfd == -1) { | ||
| // Directory does not exist, try to create | ||
| if (mkdir(RRD_SUFFIX_DIR, 0700) != 0 && errno != EEXIST) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Failed to create %s: %s\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR, strerror(errno)); | ||
| return -1; | ||
| } | ||
| dirfd = open(RRD_SUFFIX_DIR, O_NOFOLLOW | O_DIRECTORY); | ||
| if (dirfd == -1) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Could not open %s after creation: %s\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR, strerror(errno)); | ||
| return -1; | ||
| } | ||
| } | ||
| if (fstat(dirfd, &st) != 0 || !S_ISDIR(st.st_mode) || st.st_uid != uid || (st.st_mode & 0777) != 0700) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] %s not owned by current user or not mode 0700!\n", __FUNCTION__, __LINE__, RRD_SUFFIX_DIR); | ||
| close(dirfd); | ||
| return -1; | ||
| } |
| @@ -88,9 +91,23 @@ void processIssueTypeEvent(data_buf *rbuf) | |||
| } | |||
| cmdBuff->appendMode = rbuf->appendMode; | |||
| cmdBuff->mdata = (char *)calloc(1, dataMsgLen); | |||
| /* Suffix is now persisted via file, no struct field needed */ | |||
| if (cmdBuff->mdata) | |||
| { | |||
| strncpy((char *)cmdBuff->mdata, cmdMap[index], dataMsgLen); | |||
| strncpy((char *)cmdBuff->mdata, base, dataMsgLen); | |||
| /* Only persist suffix if input contains an underscore (i.e., is not just the base name) */ | |||
| if (strchr(cmdMap[index], '_') && local_suffix[0] != '\0') | |||
| { | |||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Persisting suffix: '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | |||
| if (persist_suffix_to_file(local_suffix) != 0) | |||
| { | |||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] Failed to persist suffix '%s' from input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, local_suffix, cmdMap[index], index); | |||
| } | |||
| } | |||
| else | |||
| { | |||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Not persisting suffix for input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, cmdMap[index], index); | |||
| } | |||
| processIssueType(cmdBuff); | |||
| /* Defensive: never cast a negative value, but size_t is unsigned, so only check upper bound */ | ||
| ssize_t write_len = (ssize_t)to_write; | ||
| if (write_len <= 0) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] write_len (%zd) <= 0, refusing to write\n", __FUNCTION__, __LINE__, write_len); |
There was a problem hiding this comment.
Coverity Issue - Logically dead code
Execution cannot reach this statement: "rdk_logger_msg_printf(RDK_L...".
Medium Impact, CWE-561
DEADCODE
| } | ||
| return -1; | ||
| } | ||
| ssize_t written = write(tmpfd, suffix + total_written, write_len); |
There was a problem hiding this comment.
Coverity Issue - Overflowed integer argument
"write_len", which might have underflowed, is passed to "write(tmpfd, suffix + total_written, write_len)".
High Impact, CWE-190
INTEGER_OVERFLOW
| /* Defensive: never cast a negative value, but size_t is unsigned, so only check upper bound */ | ||
| ssize_t write_len = (ssize_t)to_write; | ||
| if (write_len <= 0) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] write_len (%zd) <= 0, refusing to write\n", __FUNCTION__, __LINE__, write_len); |
There was a problem hiding this comment.
Coverity Issue - Logically dead code
Execution cannot reach this statement: "rdk_logger_msg_printf(RDK_L...".
Medium Impact, CWE-561
DEADCODE
| } | ||
| return -1; | ||
| } | ||
| ssize_t written = write(tmpfd, suffix + total_written, write_len); |
There was a problem hiding this comment.
Coverity Issue - Overflowed integer argument
"write_len", which might have underflowed, is passed to "write(tmpfd, suffix + total_written, write_len)".
High Impact, CWE-190
INTEGER_OVERFLOW
| TEST_F(SuffixUtilsTest, PersistSuffix_NormalValue) | ||
| { | ||
| persist_suffix_to_file("/tmp/rrd/rrd_suffix.txt", "_Search123"); | ||
| // Verify the file was written | ||
| FILE *fp = fopen("/tmp/rrd/rrd_suffix.txt", "r"); | ||
| ASSERT_NE(fp, nullptr); |
| persist_suffix_to_file("/tmp/rrd/rrd_suffix.txt","_OldSuffix"); | ||
| persist_suffix_to_file("/tmp/rrd/rrd_suffix.txt","_NewSuffix"); | ||
| char buf[64] = {0}; | ||
| read_suffix_from_file_to_buf("/tmp/rrd/rrd_suffix.txt",buf, sizeof(buf)); | ||
| EXPECT_STREQ(buf, "_NewSuffix"); |
| #include <fcntl.h> | ||
| #include <ctype.h> | ||
| #include <errno.h> | ||
| #include <limits.h> |
| #define RRD_SUFFIX_DIR "/tmp/rrd" | ||
| #define RRD_SUFFIX_PATH "/tmp/rrd/rrd_suffix.txt" |
| } | ||
| else | ||
| { | ||
| RDK_LOG(RDK_LOG_DEBUG, LOG_REMDEBUG, "[%s:%d]: [DEBUG] Not persisting suffix for input: '%s' (index=%d)\n", __FUNCTION__, __LINE__, cmdMap[index], index); |
| /* Defensive: never cast a negative value, but size_t is unsigned, so only check upper bound */ | ||
| ssize_t write_len = (ssize_t)to_write; | ||
| if (write_len <= 0) { | ||
| RDK_LOG(RDK_LOG_ERROR, LOG_REMDEBUG, "[%s:%d]: [ERROR] write_len (%zd) <= 0, refusing to write\n", __FUNCTION__, __LINE__, write_len); |
There was a problem hiding this comment.
Coverity Issue - Logically dead code
Execution cannot reach this statement: "rdk_logger_msg_printf(RDK_L...".
Medium Impact, CWE-561
DEADCODE
| } | ||
| return -1; | ||
| } | ||
| ssize_t written = write(tmpfd, suffix + total_written, write_len); |
There was a problem hiding this comment.
Coverity Issue - Overflowed integer argument
"write_len", which might have underflowed, is passed to "write(tmpfd, suffix + total_written, write_len)".
High Impact, CWE-190
INTEGER_OVERFLOW
No description provided.