File tree Expand file tree Collapse file tree 5 files changed +74
-0
lines changed
src/queries/security/CWE-825
test/query-tests/security/CWE-825 Expand file tree Collapse file tree 5 files changed +74
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ unsafe {
3+ std:: ptr:: drop_in_place ( ptr) ; // executes the destructor of `*ptr`
4+ }
5+
6+ // ...
7+
8+ unsafe {
9+ do_something ( & * ptr) ; // BAD: dereferences `ptr`
10+ }
Original file line number Diff line number Diff line change 1+
2+ unsafe {
3+ do_something ( & * ptr) ; // GOOD: dereferences `ptr` while it is still valid
4+ }
5+
6+ // ...
7+
8+ {
9+ std:: ptr:: drop_in_place ( ptr) ; // executes the destructor of `*ptr`
10+ }
Original file line number Diff line number Diff line change 1313| deallocation.rs:100:14:100:15 | p2 | deallocation.rs:93:21:93:42 | ...::dangling_mut | deallocation.rs:100:14:100:15 | p2 | This operation dereferences a pointer that may be $@. | deallocation.rs:93:21:93:42 | ...::dangling_mut | invalid |
1414| deallocation.rs:101:14:101:15 | p3 | deallocation.rs:94:23:94:36 | ...::null | deallocation.rs:101:14:101:15 | p3 | This operation dereferences a pointer that may be $@. | deallocation.rs:94:23:94:36 | ...::null | invalid |
1515| deallocation.rs:148:14:148:15 | p1 | deallocation.rs:145:27:145:28 | p1 | deallocation.rs:148:14:148:15 | p1 | This operation dereferences a pointer that may be $@. | deallocation.rs:145:27:145:28 | p1 | invalid |
16+ | deallocation.rs:179:18:179:20 | ptr | deallocation.rs:173:27:173:29 | ptr | deallocation.rs:179:18:179:20 | ptr | This operation dereferences a pointer that may be $@. | deallocation.rs:173:27:173:29 | ptr | invalid |
1617edges
1718| deallocation.rs:20:23:20:24 | m1 | deallocation.rs:23:13:23:14 | m1 | provenance | |
1819| deallocation.rs:20:23:20:24 | m1 | deallocation.rs:25:33:25:34 | m1 | provenance | |
3839| deallocation.rs:94:23:94:36 | ...::null | deallocation.rs:94:23:94:38 | ...::null(...) | provenance | Src:MaD:5 MaD:5 |
3940| deallocation.rs:94:23:94:38 | ...::null(...) | deallocation.rs:94:6:94:7 | p3 | provenance | |
4041| deallocation.rs:145:27:145:28 | p1 | deallocation.rs:148:14:148:15 | p1 | provenance | |
42+ | deallocation.rs:173:27:173:29 | ptr | deallocation.rs:179:18:179:20 | ptr | provenance | |
4143models
4244| 1 | Sink: lang:core; crate::ptr::read; pointer-access; Argument[0] |
4345| 2 | Sink: lang:core; crate::ptr::write; pointer-access; Argument[0] |
7678| deallocation.rs:101:14:101:15 | p3 | semmle.label | p3 |
7779| deallocation.rs:145:27:145:28 | p1 | semmle.label | p1 |
7880| deallocation.rs:148:14:148:15 | p1 | semmle.label | p1 |
81+ | deallocation.rs:173:27:173:29 | ptr | semmle.label | ptr |
82+ | deallocation.rs:179:18:179:20 | ptr | semmle.label | ptr |
7983subpaths
Original file line number Diff line number Diff line change @@ -151,3 +151,50 @@ pub fn test_ptr_drop() {
151151 println ! ( " v4 = {v4} (!)" ) ; // corrupt in practice
152152 }
153153}
154+
155+ fn do_something ( s : & String ) {
156+ println ! ( " s = {}" , s) ;
157+ }
158+
159+ fn test_qhelp_test_good ( ptr : * mut String ) {
160+ unsafe {
161+ do_something ( & * ptr) ;
162+ }
163+
164+ // ...
165+
166+ unsafe {
167+ std:: ptr:: drop_in_place ( ptr) ;
168+ }
169+ }
170+
171+ fn test_qhelp_test_bad ( ptr : * mut String ) {
172+ unsafe {
173+ std:: ptr:: drop_in_place ( ptr) ; // $ Source=drop_in_place
174+ }
175+
176+ // ...
177+
178+ unsafe {
179+ do_something ( & * ptr) ; // $ Alert[rust/access-invalid-pointer]=drop_in_place
180+ }
181+ }
182+
183+ pub fn test_qhelp_tests ( ) {
184+ let layout = std:: alloc:: Layout :: new :: < [ String ; 2 ] > ( ) ;
185+ unsafe {
186+ let ptr = std:: alloc:: alloc ( layout) ;
187+ let ptr_s = ptr as * mut [ String ; 2 ] ;
188+ let ptr1 = & raw mut ( * ptr_s) [ 0 ] ;
189+ let ptr2 = & raw mut ( * ptr_s) [ 1 ] ;
190+
191+ * ptr1 = String :: from ( "123" ) ;
192+ * ptr2 = String :: from ( "456" ) ;
193+
194+ test_qhelp_test_good ( ptr1) ;
195+
196+ test_qhelp_test_bad ( ptr2) ;
197+
198+ std:: alloc:: dealloc ( ptr, layout) ;
199+ }
200+ }
Original file line number Diff line number Diff line change @@ -123,6 +123,9 @@ fn main() {
123123 println ! ( "test_ptr_drop:" ) ;
124124 test_ptr_drop ( ) ;
125125
126+ println ! ( "test_qhelp_tests:" ) ;
127+ test_qhelp_tests ( ) ;
128+
126129 // ---
127130
128131 println ! ( "test_local_dangling:" ) ;
You can’t perform that action at this time.
0 commit comments