-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial version of CWE560 check * CWE560 identifies calls to umask, missing the check of the umask calls. * Initial version of CWE560 check * CWE560 identifies calls to umask, missing the check of the umask calls. * [cwe560] works for x64, fix function check_umask_call to detect on other arches * Initial version of CWE560 check * CWE560 identifies calls to umask, missing the check of the umask calls. * Initial version of CWE560 check * [cwe560] works for x64, fix function check_umask_call to detect on other arches * Now working on the other architectures * Refactored version of check for CWE 560 that work on several architectures. Added first unit tests for the checkers code base * Fixes some dune warnings. * Added CWE 560 to CHANGES.md. Fixes another dune warning. * Requested change: Private module as a wrapper for unit tests
- Loading branch information
1 parent
eaf5172
commit 89c388b
Showing
13 changed files
with
207 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
open Core_kernel | ||
open Bap.Std | ||
|
||
let name = "CWE560" | ||
let version = "0.1" | ||
|
||
let upper_bound_of_correct_umask_arg_value = 100 | ||
let upper_bound_of_correct_chmod_arg_value = 1000 | ||
|
||
let collect_int_values = Exp.fold ~init:[] (object | ||
inherit [word list] Exp.visitor | ||
method! enter_int x addrs = x :: addrs | ||
end) | ||
|
||
let is_chmod_style_arg umask_arg = | ||
umask_arg > upper_bound_of_correct_umask_arg_value && umask_arg < upper_bound_of_correct_chmod_arg_value | ||
|
||
let check_umask_arg tid_map blk w = | ||
try | ||
let umask_arg = Word.to_int_exn w in | ||
if is_chmod_style_arg umask_arg then | ||
Log_utils.warn "[%s] {%s} (Use of umask() with chmod-style Argument) Function %s calls umask with argument %d" | ||
name | ||
version | ||
(Address_translation.translate_tid_to_assembler_address_string (Term.tid blk) tid_map) | ||
umask_arg | ||
with _ -> Log_utils.error "Caught exception in module [CWE560]." | ||
|
||
let check_umask_callsite tid_map blk = | ||
Seq.iter (Term.enum def_t blk) ~f:(fun d -> | ||
let rhs = Def.rhs d in | ||
let int_values = collect_int_values rhs in | ||
List.iter int_values ~f:(fun x -> check_umask_arg tid_map blk x) | ||
) | ||
|
||
let blk_calls_umask sym_umask blk = | ||
Term.enum jmp_t blk | ||
|> Seq.exists ~f:(fun callsite -> Symbol_utils.calls_callsite_symbol callsite sym_umask) | ||
|
||
let check_subfunction program tid_map sym_umask sub = | ||
if Symbol_utils.sub_calls_symbol program sub "umask" then | ||
Term.enum blk_t sub | ||
|> Seq.filter ~f:(fun blk -> blk_calls_umask sym_umask blk) | ||
|> Seq.iter ~f:(fun blk -> check_umask_callsite tid_map blk) | ||
else | ||
() | ||
|
||
let check_subfunctions program tid_map sym_umask = | ||
Seq.iter (Term.enum sub_t program) ~f:(fun sub -> check_subfunction program tid_map sym_umask sub) | ||
|
||
let check_cwe program _ tid_map _ _ = | ||
let sym = Symbol_utils.get_symbol_of_string program "umask" in | ||
match sym with | ||
| None -> () | ||
| Some sym_umask -> check_subfunctions program tid_map sym_umask | ||
|
||
|
||
(* Functions made available for unit tests *) | ||
module Private = struct | ||
let is_chmod_style_arg = is_chmod_style_arg | ||
end |
This file contains 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
(** CWE-560 (Use of umask() with chmod-style Argument) | ||
https://cwe.mitre.org/data/definitions/560.html | ||
The program uses the system call umask(2) with arguements for chmod(2). For instance, | ||
instead of a reasonable value like 0022 a value like 0666 is passed. This may result wrong | ||
read and/or write access to files and directories, which could be utilized to bypass | ||
protection mechanisms. | ||
This check looks for umask calls and checks if they have a reasonable value, i.e. smaller than | ||
a certain value, currently set to 1000 and greater than a reasonable value for umask, currently set to 100. | ||
A future version should include a proper data flow analysis to track the first argument since the current | ||
version considers all immediate values of an umask callsite's basic block. | ||
*) | ||
val name : string | ||
val version : string | ||
|
||
val check_cwe : Bap.Std.program Bap.Std.term -> Bap.Std.project -> Bap.Std.word Bap.Std.Tid.Map.t -> string list list -> string list -> unit | ||
|
||
|
||
(* functions made available for unit tests: *) | ||
module Private : sig | ||
val is_chmod_style_arg : int -> bool | ||
end |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import unittest | ||
import cwe_checker_testlib | ||
|
||
|
||
class TestCwe560(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.target = '560' | ||
self.string = b'Use of umask() with chmod-style Argument' | ||
|
||
@unittest.skip("Args of umask to not seem to be found by BAP. Investigate in the future") | ||
def test_cwe560_01_arm(self): | ||
expect_res = 1 | ||
res = cwe_checker_testlib.execute_and_check_occurence(self.target, self.target, 'arm', self.string) | ||
self.assertEqual(res, expect_res) | ||
|
||
def test_cwe560_01_x86(self): | ||
expect_res = 1 | ||
res = cwe_checker_testlib.execute_and_check_occurence(self.target, self.target, 'x86', self.string) | ||
self.assertEqual(res, expect_res) | ||
|
||
def test_cwe560_01_x64(self): | ||
expect_res = 1 | ||
res = cwe_checker_testlib.execute_and_check_occurence(self.target, self.target, 'x64', self.string) | ||
self.assertEqual(res, expect_res) | ||
|
||
@unittest.skip("Depends on proper MIPS support in BAP") | ||
def test_cwe560_01_mips(self): | ||
expect_res = 1 | ||
res = cwe_checker_testlib.execute_and_check_occurence(self.target, self.target, 'mips', self.string) | ||
self.assertEqual(res, expect_res) | ||
|
||
def test_cwe560_01_ppc(self): | ||
expect_res = 1 | ||
res = cwe_checker_testlib.execute_and_check_occurence(self.target, self.target, 'ppc', self.string) | ||
self.assertEqual(res, expect_res) |
This file contains 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
void umask_incorrect(){ | ||
umask(666); | ||
int fd = open("some_random_file", O_CREAT|O_WRONLY, 0666); | ||
close(fd); | ||
} | ||
|
||
void umask_correct(){ | ||
umask(022); | ||
int fd = open("some_random_file", O_CREAT|O_WRONLY, 0666); | ||
close(fd); | ||
} | ||
|
||
int main(){ | ||
umask_correct(); | ||
umask_incorrect(); | ||
return 0; | ||
} |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
open Bap.Std | ||
open Core_kernel | ||
open Cwe_checker_core | ||
|
||
let check msg x = Alcotest.(check bool) msg true x | ||
|
||
let test_is_chmod_style_arg_with_umask_arg () : unit = | ||
let res = Cwe_560.Private.is_chmod_style_arg 022 in | ||
check "empty" (res = false) | ||
|
||
let test_is_chmod_style_arg_with_chmod_arg () : unit = | ||
let res = Cwe_560.Private.is_chmod_style_arg 666 in | ||
check "empty" (res = true) | ||
|
||
let tests = [ | ||
"Is chmod style argument with umask argument?", `Quick, test_is_chmod_style_arg_with_umask_arg; | ||
"Is chmod style argument with chmod argument?", `Quick, test_is_chmod_style_arg_with_chmod_arg; | ||
] |
This file contains 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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
open Bap.Std | ||
open Core_kernel | ||
|
||
val tests: unit Alcotest.test_case list |
This file contains 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