Skip to content

Commit e263116

Browse files
committed
Rust: Model std::net and tokio::net.
1 parent 038b8b5 commit e263116

File tree

4 files changed

+53
-18
lines changed

4 files changed

+53
-18
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/rust-all
4+
extensible: sourceModel
5+
data:
6+
- ["lang:std", "<crate::net::tcp::TcpStream>::connect", "ReturnValue.Field[crate::result::Result::Ok(0)]", "remote", "manual"]
7+
- ["lang:std", "<crate::net::tcp::TcpStream>::connect_timeout", "ReturnValue.Field[crate::result::Result::Ok(0)]", "remote", "manual"]
8+
- addsTo:
9+
pack: codeql/rust-all
10+
extensible: summaryModel
11+
data:
12+
- ["lang:std", "<crate::net::tcp::TcpStream>::try_clone", "Argument[self]", "ReturnValue.Field[crate::result::Result::Ok(0)]", "taint", "manual"]
13+
- ["lang:std", "<crate::net::tcp::TcpStream as crate::io::Read>::read", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
14+
- ["lang:std", "<crate::net::tcp::TcpStream as crate::io::Read>::read_to_string", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
15+
- ["lang:std", "<crate::net::tcp::TcpStream as crate::io::Read>::read_to_end", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
16+
- ["lang:std", "<crate::net::tcp::TcpStream as crate::io::Read>::read_exact", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
extensions:
2+
- addsTo:
3+
pack: codeql/rust-all
4+
extensible: sourceModel
5+
data:
6+
- ["repo:https://github.com/tokio-rs/tokio:tokio", "<crate::net::tcp::stream::TcpStream>::connect", "ReturnValue.Future.Field[crate::result::Result::Ok(0)]", "remote", "manual"]
7+
- addsTo:
8+
pack: codeql/rust-all
9+
extensible: summaryModel
10+
data:
11+
- ["repo:https://github.com/tokio-rs/tokio:tokio", "<crate::net::tcp::stream::TcpStream>::peek", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
12+
- ["repo:https://github.com/tokio-rs/tokio:tokio", "crate::io::util::async_read_ext::AsyncReadExt::read", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
13+
- ["repo:https://github.com/tokio-rs/tokio:tokio", "<crate::net::tcp::stream::TcpStream>::try_read", "Argument[self]", "Argument[0].Reference", "taint", "manual"]
14+
- ["repo:https://github.com/tokio-rs/tokio:tokio", "<crate::net::tcp::stream::TcpStream>::try_read_buf", "Argument[self]", "Argument[0].Reference", "taint", "manual"]

rust/ql/test/library-tests/dataflow/sources/TaintSources.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| test.rs:74:26:74:37 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
2121
| test.rs:77:26:77:37 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
2222
| test.rs:80:24:80:35 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
23+
| test.rs:99:18:99:47 | ...::connect | Flow source 'RemoteSource' of type remote (DEFAULT). |
2324
| test.rs:113:31:113:42 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
2425
| test.rs:120:31:120:42 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
2526
| test.rs:210:22:210:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
@@ -68,4 +69,8 @@
6869
| test.rs:574:21:574:41 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
6970
| test.rs:575:21:575:41 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
7071
| test.rs:583:21:583:41 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
72+
| test.rs:600:26:600:53 | ...::connect | Flow source 'RemoteSource' of type remote (DEFAULT). |
73+
| test.rs:619:26:619:61 | ...::connect_timeout | Flow source 'RemoteSource' of type remote (DEFAULT). |
74+
| test.rs:671:28:671:57 | ...::connect | Flow source 'RemoteSource' of type remote (DEFAULT). |
75+
| test.rs:753:22:753:49 | ...::connect | Flow source 'RemoteSource' of type remote (DEFAULT). |
7176
| test.rs:775:16:775:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |

rust/ql/test/library-tests/dataflow/sources/test.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ async fn test_hyper_http(case: i64) -> Result<(), Box<dyn std::error::Error>> {
9696

9797
// create the connection
9898
println!("connecting to {}...", address);
99-
let stream = tokio::net::TcpStream::connect(address).await?;
99+
let stream = tokio::net::TcpStream::connect(address).await?; // $ Alert[rust/summary/taint-sources]
100100
let io = hyper_util::rt::TokioIo::new(stream);
101101
let (mut sender, conn) = hyper::client::conn::http1::handshake(io).await?;
102102

@@ -597,26 +597,26 @@ async fn test_std_tcpstream(case: i64) -> std::io::Result<()> { // Result<(), Bo
597597

598598
if case == 1 {
599599
// create the connection
600-
let mut stream = std::net::TcpStream::connect(address)?;
600+
let mut stream = std::net::TcpStream::connect(address)?; // $ Alert[rust/summary/taint-sources]
601601

602602
// send request
603603
let _ = stream.write_all(b"GET / HTTP/1.1\nHost:example.com\n\n");
604604

605605
// read response
606606
let mut buffer = vec![0; 32 * 1024];
607-
let _ = stream.read(&mut buffer); // $ MISSING: Alert[rust/summary/taint-sources]
607+
let _ = stream.read(&mut buffer);
608608

609609
println!("data = {:?}", buffer);
610-
sink(&buffer); // $ MISSING: hasTaintFlow
611-
sink(buffer[0]); // $ MISSING: hasTaintFlow
610+
sink(&buffer); // $ hasTaintFlow=address
611+
sink(buffer[0]); // $ hasTaintFlow=address
612612

613613
let buffer_string = String::from_utf8_lossy(&buffer);
614614
println!("string = {}", buffer_string);
615615
sink(buffer_string); // $ MISSING: hasTaintFlow
616616
} else {
617617
// create the connection
618618
let sock_addr = address.to_socket_addrs().unwrap().next().unwrap();
619-
let mut stream = std::net::TcpStream::connect_timeout(&sock_addr, std::time::Duration::new(1, 0))?;
619+
let mut stream = std::net::TcpStream::connect_timeout(&sock_addr, std::time::Duration::new(1, 0))?; // $ Alert[rust/summary/taint-sources]
620620

621621
// send request
622622
let _ = stream.write_all(b"GET / HTTP/1.1\nHost:example.com\n\n");
@@ -627,14 +627,14 @@ async fn test_std_tcpstream(case: i64) -> std::io::Result<()> { // Result<(), Bo
627627
let mut reader = std::io::BufReader::new(stream).take(256);
628628
let mut line = String::new();
629629
loop {
630-
match reader.read_line(&mut line) { // $ MISSING: Alert[rust/summary/taint-sources]
630+
match reader.read_line(&mut line) {
631631
Ok(0) => {
632632
println!("end");
633633
break;
634634
}
635635
Ok(_n) => {
636636
println!("line = {}", line);
637-
sink(&line); // $ MISSING: hasTaintFlow
637+
sink(&line); // $ hasTaintFlow=&sock_addr
638638
line.clear();
639639
}
640640
Err(e) => {
@@ -668,27 +668,27 @@ async fn test_tokio_tcpstream(case: i64) -> std::io::Result<()> {
668668

669669
// create the connection
670670
println!("connecting to {}...", address);
671-
let mut tokio_stream = tokio::net::TcpStream::connect(address).await?;
671+
let mut tokio_stream = tokio::net::TcpStream::connect(address).await?; // $ Alert[rust/summary/taint-sources]
672672

673673
// send request
674674
tokio_stream.write_all(b"GET / HTTP/1.1\nHost:example.com\n\n").await?;
675675

676676
if case == 1 {
677677
// peek response
678678
let mut buffer1 = vec![0; 2 * 1024];
679-
let _ = tokio_stream.peek(&mut buffer1).await?; // $ MISSING: Alert[rust/summary/taint-sources]
679+
let _ = tokio_stream.peek(&mut buffer1).await?;
680680

681681
// read response
682682
let mut buffer2 = vec![0; 2 * 1024];
683-
let n2 = tokio_stream.read(&mut buffer2).await?; // $ MISSING: Alert[rust/summary/taint-sources]
683+
let n2 = tokio_stream.read(&mut buffer2).await?;
684684

685685
println!("buffer1 = {:?}", buffer1);
686-
sink(&buffer1); // $ MISSING: hasTaintFlow
687-
sink(buffer1[0]); // $ MISSING: hasTaintFlow
686+
sink(&buffer1); // $ hasTaintFlow=address
687+
sink(buffer1[0]); // $ hasTaintFlow=address
688688

689689
println!("buffer2 = {:?}", buffer2);
690-
sink(&buffer2); // $ MISSING: hasTaintFlow
691-
sink(buffer2[0]); // $ MISSING: hasTaintFlow
690+
sink(&buffer2); // $ hasTaintFlow=address
691+
sink(buffer2[0]); // $ hasTaintFlow=address
692692

693693
let buffer_string = String::from_utf8_lossy(&buffer2[..n2]);
694694
println!("string = {}", buffer_string);
@@ -703,7 +703,7 @@ async fn test_tokio_tcpstream(case: i64) -> std::io::Result<()> {
703703
}
704704
Ok(_n) => {
705705
println!("buffer = {:?}", buffer);
706-
sink(&buffer); // $ MISSING: hasTaintFlow
706+
sink(&buffer); // $ hasTaintFlow=address
707707
break; // (or we could wait for more data)
708708
}
709709
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
@@ -726,7 +726,7 @@ async fn test_tokio_tcpstream(case: i64) -> std::io::Result<()> {
726726
}
727727
Ok(_n) => {
728728
println!("buffer = {:?}", buffer);
729-
sink(&buffer); // $ MISSING: hasTaintFlow
729+
sink(&buffer); // $ hasTaintFlow=address
730730
break; // (or we could wait for more data)
731731
}
732732
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
@@ -750,7 +750,7 @@ async fn test_std_to_tokio_tcpstream() -> std::io::Result<()> {
750750

751751
// create the connection
752752
println!("connecting to {}...", address);
753-
let std_stream = std::net::TcpStream::connect(address)?;
753+
let std_stream = std::net::TcpStream::connect(address)?; // $ Alert[rust/summary/taint-sources]
754754

755755
// convert to tokio stream
756756
std_stream.set_nonblocking(true)?;

0 commit comments

Comments
 (0)