forked from NVIDIA-AI-Blueprints/vulnerability-analysis
-
Notifications
You must be signed in to change notification settings - Fork 15
Appeng 5612 and 5613 #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RedTanny
wants to merge
6
commits into
RHEcosystemAppEng:main
Choose a base branch
from
RedTanny:APPENG-5612-5613
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,551
−58
Open
Appeng 5612 and 5613 #284
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3330155
feat: C CCA argument-count pre-filter + Go CCA sub-package awareness
RedTanny 4975819
CodeReview: Address PR #261 review comments for Go CCA
RedTanny 341cffe
fix: Revert is_package_imported regex - keep trailing .* for versione…
RedTanny 051d86c
fix: Restore parse_cpe with CPE 2.3 validation and escape-aware split…
RedTanny 524b011
feat: Add Function Locator status hooks to prevent false VALIDATED ob…
RedTanny bd9a9b8
fix unit tests
RedTanny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,140 @@ def _remove_c_comments(code: str) -> str: | |
| code = re.sub(r'//.*', '', code) | ||
| return code | ||
|
|
||
|
|
||
| def _count_c_declared_params(func_doc: Document) -> int | None: | ||
| """Count declared parameters in a C function definition. | ||
| Returns None if the function signature cannot be parsed (e.g. macros, variadic).""" | ||
| code = func_doc.page_content.strip().replace('\r\n', '\n') | ||
| m = _FUNCTION_PATTERN_REGEX.search(code, timeout=REGEX_TIMEOUT_SECONDS) | ||
| if not m: | ||
| return None | ||
| params = m.group('params').strip() | ||
| if not params or params == 'void': | ||
| return 0 | ||
| if '...' in params: | ||
| return None | ||
| depth = 0 | ||
| count = 1 | ||
| for ch in params: | ||
| if ch == '(': | ||
| depth += 1 | ||
| elif ch == ')': | ||
| depth -= 1 | ||
| elif ch == ',' and depth == 0: | ||
| count += 1 | ||
| return count | ||
|
|
||
|
|
||
| def _extract_call_site_args_str(function_body: str, start_pos: int) -> str | None: | ||
| """Extract the arguments string from a call site starting at the opening paren. | ||
| Returns None if the call site cannot be parsed. | ||
|
|
||
| Correctly handles string literals, char literals, and nested parentheses. | ||
| """ | ||
| depth = 0 | ||
| end_pos = -1 | ||
| in_string = False | ||
| in_char = False | ||
| i = start_pos | ||
| while i < len(function_body): | ||
| ch = function_body[i] | ||
| if in_string: | ||
| if ch == '\\' and i + 1 < len(function_body): | ||
| i += 2 | ||
| continue | ||
| if ch == '"': | ||
| in_string = False | ||
| elif in_char: | ||
| if ch == '\\' and i + 1 < len(function_body): | ||
| i += 2 | ||
| continue | ||
| if ch == "'": | ||
| in_char = False | ||
| else: | ||
| if ch == '"': | ||
| in_string = True | ||
| elif ch == "'": | ||
| in_char = True | ||
| elif ch == '(': | ||
| depth += 1 | ||
| elif ch == ')': | ||
| depth -= 1 | ||
| if depth == 0: | ||
| end_pos = i | ||
| break | ||
| i += 1 | ||
| if end_pos == -1: | ||
| return None | ||
| return function_body[start_pos + 1:end_pos].strip() | ||
|
|
||
|
|
||
| def _count_args_in_str(args_str: str) -> int: | ||
| """Count the number of arguments in an argument string. | ||
|
|
||
| Correctly handles commas inside string literals, char literals, and nested parentheses. | ||
| """ | ||
| if not args_str: | ||
| return 0 | ||
| depth = 0 | ||
| count = 1 | ||
| in_string = False | ||
| in_char = False | ||
| i = 0 | ||
| while i < len(args_str): | ||
| ch = args_str[i] | ||
| if in_string: | ||
| if ch == '\\' and i + 1 < len(args_str): | ||
| i += 2 | ||
| continue | ||
| if ch == '"': | ||
| in_string = False | ||
| elif in_char: | ||
| if ch == '\\' and i + 1 < len(args_str): | ||
| i += 2 | ||
| continue | ||
| if ch == "'": | ||
| in_char = False | ||
| else: | ||
| if ch == '"': | ||
| in_string = True | ||
| elif ch == "'": | ||
| in_char = True | ||
| elif ch == '(': | ||
| depth += 1 | ||
| elif ch == ')': | ||
| depth -= 1 | ||
| elif ch == ',' and depth == 0: | ||
| count += 1 | ||
| i += 1 | ||
| return count | ||
|
|
||
|
|
||
| def _count_all_call_site_args(function_body: str, func_name: str) -> list[int]: | ||
| """Count arguments at ALL call sites of func_name(...) in function_body. | ||
| Returns a list of argument counts for each call site found. | ||
|
|
||
| Correctly handles commas inside string literals, char literals, and nested parentheses. | ||
| """ | ||
| pattern = re.compile(r'\b' + re.escape(func_name) + r'\s*\(') | ||
| results = [] | ||
| for m in pattern.finditer(function_body): | ||
| start = m.end() - 1 | ||
| args_str = _extract_call_site_args_str(function_body, start) | ||
| if args_str is not None: | ||
| results.append(_count_args_in_str(args_str)) | ||
| return results | ||
|
|
||
|
|
||
| def _count_call_site_args(function_body: str, func_name: str) -> int | None: | ||
| """Count arguments at the first call site of func_name(...) in function_body. | ||
| Returns None if no call site is found. | ||
|
|
||
| Correctly handles commas inside string literals, char literals, and nested parentheses. | ||
| """ | ||
| counts = _count_all_call_site_args(function_body, func_name) | ||
| return counts[0] if counts else None | ||
|
|
||
| # Compile the function pattern with recursive param matching | ||
| _FUNCTION_PATTERN_REGEX = regex.compile(r''' | ||
| ^[ \t]* # Leading indentation | ||
|
|
@@ -533,15 +667,15 @@ def search_for_called_function( | |
| self, | ||
| caller_function: Document, | ||
| callee_function_name: str, | ||
| callee_function_package: str, # For C, this is usually the file or library name | ||
| callee_function: Document, | ||
| callee_function_package: str, | ||
|
Comment on lines
+670
to
+671
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RedTanny Until now it was different then the signature of the Parent ABC huh? |
||
| code_documents: list[Document], | ||
| type_documents: list[Document], | ||
| callee_function_file_name: str, | ||
| fields_of_types: dict[tuple, list[tuple]], | ||
| functions_local_variables_index: dict[str, dict], | ||
| documents_of_functions: list[Document] = None, | ||
| type_inheritance: dict[Tuple[str, str], List[Tuple[str, str]]] = None, | ||
| callee_function:Document = None | ||
| ) -> bool: | ||
| """ | ||
| Returns True if caller_function calls callee_function (directly or via struct/function pointer). | ||
|
|
@@ -551,7 +685,22 @@ def search_for_called_function( | |
| # 2. Direct call: callee_function_name( | ||
| direct_call_pattern = re.compile(r'\b' + re.escape(callee_function_name) + r'\s*\(') | ||
| if direct_call_pattern.search(function_body): | ||
| return True | ||
| if callee_function is not None: | ||
| declared = _count_c_declared_params(callee_function) | ||
| if declared is None: | ||
| return True | ||
| all_call_args = _count_all_call_site_args(function_body, callee_function_name) | ||
| if not all_call_args: | ||
| return True | ||
| if declared in all_call_args: | ||
| return True | ||
| logger.debug( | ||
| "Argument count mismatch for %s: declared=%d, call_sites=%s in %s — rejecting false match", | ||
| callee_function_name, declared, all_call_args, | ||
| caller_function.metadata.get('source', '?')) | ||
| return False | ||
| else: | ||
| return True | ||
|
|
||
| # 3. Struct member or pointer call: obj->callee_function_name( or obj.callee_function_name( | ||
| member_call_pattern = re.compile( | ||
|
|
@@ -750,9 +899,6 @@ def is_call_allowed(self, pkg_docs: list[Document], caller_function: Document, c | |
|
|
||
| callee_name = self.get_function_name(callee_function) | ||
|
|
||
| if callee_name == "do_shell": | ||
| print(f"callee_name: {callee_name}") | ||
|
|
||
| if callee_name in caller_functions: | ||
| doc = caller_functions[callee_name] | ||
| # if static and in same file → call is allowed | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RedTanny Better relocate this regex pattern to the top of the module, so it will be before the function that uses that, for a better readability of the logic and code.