Skip to content

Commit 17ead39

Browse files
ioanachircaacatangiu
authored andcommitted
micro_http: check arithmetic operations
Signed-off-by: Ioana Chirca <[email protected]> Signed-off-by: YUAN LYU <[email protected]>
1 parent 9830b66 commit 17ead39

File tree

4 files changed

+125
-33
lines changed

4 files changed

+125
-33
lines changed

src/common/mod.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ pub enum RequestError {
2828
InvalidHeader,
2929
/// The Request is invalid and cannot be served.
3030
InvalidRequest,
31+
/// Overflow occurred when parsing a request.
32+
Overflow,
33+
/// Underflow occurred when parsing a request.
34+
Underflow,
3135
}
3236

3337
impl Display for RequestError {
@@ -39,6 +43,8 @@ impl Display for RequestError {
3943
Self::UnsupportedHeader => write!(f, "Unsupported header."),
4044
Self::InvalidHeader => write!(f, "Invalid header."),
4145
Self::InvalidRequest => write!(f, "Invalid request."),
46+
Self::Overflow => write!(f, "Overflow occurred when parsing a request."),
47+
Self::Underflow => write!(f, "Underflow occurred when parsing a request."),
4248
}
4349
}
4450
}
@@ -91,6 +97,10 @@ pub enum ServerError {
9197
ConnectionError(ConnectionError),
9298
/// Server maximum capacity has been reached.
9399
ServerFull,
100+
/// Overflow occured while processing messages.
101+
Overflow,
102+
/// Underflow occured while processing mesagges.
103+
Underflow,
94104
}
95105

96106
impl Display for ServerError {
@@ -99,6 +109,8 @@ impl Display for ServerError {
99109
Self::IOError(inner) => write!(f, "IO error: {}", inner),
100110
Self::ConnectionError(inner) => write!(f, "Connection error: {}", inner),
101111
Self::ServerFull => write!(f, "Server is full."),
112+
Self::Overflow => write!(f, "Overflow occured while processing messages."),
113+
Self::Underflow => write!(f, "Underflow occured while processing messages."),
102114
}
103115
}
104116
}
@@ -250,9 +262,11 @@ mod tests {
250262
fn eq(&self, other: &Self) -> bool {
251263
use self::ConnectionError::*;
252264
match (self, other) {
253-
(ParseError(_), ParseError(_)) => true,
265+
(ParseError(ref e), ParseError(ref other_e)) => e.eq(other_e),
254266
(ConnectionClosed, ConnectionClosed) => true,
255-
(StreamError(_), StreamError(_)) => true,
267+
(StreamError(ref e), StreamError(ref other_e)) => {
268+
format!("{}", e).eq(&format!("{}", other_e))
269+
}
256270
(InvalidWrite, InvalidWrite) => true,
257271
_ => false,
258272
}
@@ -308,30 +322,38 @@ mod tests {
308322

309323
#[test]
310324
fn test_display_request_error() {
325+
assert_eq!(
326+
format!("{}", RequestError::InvalidHeader),
327+
"Invalid header."
328+
);
311329
assert_eq!(
312330
format!("{}", RequestError::InvalidHttpMethod("test")),
313331
"Invalid HTTP Method: test"
314332
);
333+
assert_eq!(
334+
format!("{}", RequestError::InvalidHttpVersion("test")),
335+
"Invalid HTTP Version: test"
336+
);
337+
assert_eq!(
338+
format!("{}", RequestError::InvalidRequest),
339+
"Invalid request."
340+
);
315341
assert_eq!(
316342
format!("{}", RequestError::InvalidUri("test")),
317343
"Invalid URI: test"
318344
);
319345
assert_eq!(
320-
format!("{}", RequestError::InvalidHttpVersion("test")),
321-
"Invalid HTTP Version: test"
346+
format!("{}", RequestError::Overflow),
347+
"Overflow occurred when parsing a request."
322348
);
323349
assert_eq!(
324-
format!("{}", RequestError::InvalidHeader),
325-
"Invalid header."
350+
format!("{}", RequestError::Underflow),
351+
"Underflow occurred when parsing a request."
326352
);
327353
assert_eq!(
328354
format!("{}", RequestError::UnsupportedHeader),
329355
"Unsupported header."
330356
);
331-
assert_eq!(
332-
format!("{}", RequestError::InvalidRequest),
333-
"Invalid request."
334-
);
335357
}
336358

337359
#[test]
@@ -377,6 +399,14 @@ mod tests {
377399
),
378400
"IO error: Resource temporarily unavailable (os error 11)"
379401
);
402+
assert_eq!(
403+
format!("{}", ServerError::Overflow),
404+
"Overflow occured while processing messages."
405+
);
406+
assert_eq!(
407+
format!("{}", ServerError::Underflow),
408+
"Underflow occured while processing messages."
409+
);
380410
}
381411

382412
#[test]

src/request.rs

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ use crate::common::{Body, Method, Version};
99

1010
pub use crate::common::RequestError;
1111

12-
/// Finds the first occurrence of `sequence` in the `bytes` slice.
12+
// This type represents the RequestLine raw parts: method, uri and version.
13+
type RequestLineParts<'a> = (&'a [u8], &'a [u8], &'a [u8]);
14+
15+
/// Finds the first occurence of `sequence` in the `bytes` slice.
1316
///
1417
/// Returns the starting position of the `sequence` in `bytes` or `None` if the
1518
/// `sequence` is not found.
@@ -58,13 +61,15 @@ impl Uri {
5861
const HTTP_SCHEME_PREFIX: &str = "http://";
5962

6063
if self.string.starts_with(HTTP_SCHEME_PREFIX) {
64+
// Slice access is safe because we checked above that `self.string` size <= `HTTP_SCHEME_PREFIX.len()`.
6165
let without_scheme = &self.string[HTTP_SCHEME_PREFIX.len()..];
6266
if without_scheme.is_empty() {
6367
return "";
6468
}
6569
// The host in this case includes the port and contains the bytes after http:// up to
6670
// the next '/'.
6771
match without_scheme.bytes().position(|byte| byte == b'/') {
72+
// Slice access is safe because `position` validates that `len` is a valid index.
6873
Some(len) => &without_scheme[len..],
6974
None => "",
7075
}
@@ -87,24 +92,36 @@ pub struct RequestLine {
8792
}
8893

8994
impl RequestLine {
90-
fn parse_request_line(request_line: &[u8]) -> (&[u8], &[u8], &[u8]) {
95+
fn parse_request_line(
96+
request_line: &[u8],
97+
) -> std::result::Result<RequestLineParts, RequestError> {
9198
if let Some(method_end) = find(request_line, &[SP]) {
99+
// The slice access is safe because `find` validates that `method_end` < `request_line` size.
92100
let method = &request_line[..method_end];
93101

94-
let uri_and_version = &request_line[(method_end + 1)..];
102+
// `uri_start` <= `request_line` size.
103+
let uri_start = method_end.checked_add(1).ok_or(RequestError::Overflow)?;
104+
105+
// Slice access is safe because `uri_start` <= `request_line` size.
106+
// If `uri_start` == `request_line` size, then `uri_and_version` will be an empty slice.
107+
let uri_and_version = &request_line[uri_start..];
95108

96109
if let Some(uri_end) = find(uri_and_version, &[SP]) {
110+
// Slice access is safe because `find` validates that `uri_end` < `uri_and_version` size.
97111
let uri = &uri_and_version[..uri_end];
98112

99-
let version = &uri_and_version[(uri_end + 1)..];
113+
// `version_start` <= `uri_and_version` size.
114+
let version_start = uri_end.checked_add(1).ok_or(RequestError::Overflow)?;
100115

101-
return (method, uri, version);
102-
}
116+
// Slice access is safe because `version_start` <= `uri_and_version` size.
117+
let version = &uri_and_version[version_start..];
103118

104-
return (method, uri_and_version, b"");
119+
return Ok((method, uri, version));
120+
}
105121
}
106122

107-
(b"", b"", b"")
123+
// Request Line can be valid only if it contains the method, uri and version separated with SP.
124+
Err(RequestError::InvalidRequest)
108125
}
109126

110127
/// Tries to parse a byte stream in a request line. Fails if the request line is malformed.
@@ -114,7 +131,7 @@ impl RequestLine {
114131
/// `InvalidHttpVersion` is returned if the specified HTTP version is unsupported.
115132
/// `InvalidUri` is returned if the specified Uri is not valid.
116133
pub fn try_from(request_line: &[u8]) -> Result<Self, RequestError> {
117-
let (method, uri, version) = Self::parse_request_line(request_line);
134+
let (method, uri, version) = Self::parse_request_line(request_line)?;
118135

119136
Ok(Self {
120137
method: Method::try_from(method)?,
@@ -124,9 +141,10 @@ impl RequestLine {
124141
}
125142

126143
// Returns the minimum length of a valid request. The request must contain
127-
// the method (GET), the URI (minmum 1 character), the HTTP version(HTTP/DIGIT.DIGIT) and
144+
// the method (GET), the URI (minimum 1 character), the HTTP version(HTTP/DIGIT.DIGIT) and
128145
// 2 separators (SP).
129146
fn min_len() -> usize {
147+
// Addition is safe because these are small constants.
130148
Method::Get.raw().len() + 1 + Version::Http10.raw().len() + 2
131149
}
132150
}
@@ -148,7 +166,10 @@ impl Request {
148166
/// The byte slice is expected to have the following format: </br>
149167
/// * Request Line: "GET SP Request-uri SP HTTP/1.0 CRLF" - Mandatory </br>
150168
/// * Request Headers "<headers> CRLF"- Optional </br>
169+
/// * Empty Line "CRLF" </br>
151170
/// * Entity Body - Optional </br>
171+
/// The request headers and the entity body are not parsed and None is returned because
172+
/// these are not used by the MMDS server.
152173
/// The only supported method is GET and the HTTP protocol is expected to be HTTP/1.0
153174
/// or HTTP/1.1.
154175
///
@@ -171,6 +192,7 @@ impl Request {
171192
None => return Err(RequestError::InvalidRequest),
172193
};
173194

195+
// Slice access is safe because `find` validates that `request_line_end` < `byte_stream` size.
174196
let request_line_bytes = &byte_stream[..request_line_end];
175197
if request_line_bytes.len() < RequestLine::min_len() {
176198
return Err(RequestError::InvalidRequest);
@@ -191,8 +213,20 @@ impl Request {
191213
Some(headers_end) => {
192214
// Parse the request headers.
193215
// Start by removing the leading CR LF from them.
194-
let headers_and_body = &byte_stream[(request_line_end + CRLF_LEN)..];
216+
let headers_start = request_line_end
217+
.checked_add(CRLF_LEN)
218+
.ok_or(RequestError::Overflow)?;
219+
// Slice access is safe because starting from `request_line_end` there are at least two CRLF
220+
// (enforced by `find` at the start of this method).
221+
let headers_and_body = &byte_stream[headers_start..];
222+
// Because we advanced the start with CRLF_LEN, we now have to subtract CRLF_LEN
223+
// from the end in order to keep the same window.
224+
// Underflow is not possible here because `byte_stream[request_line_end..]` starts with CR LF,
225+
// so `headers_end` can be either zero (this case is treated separately in the first match arm)
226+
// or >= 3 (current case).
195227
let headers_end = headers_end - CRLF_LEN;
228+
// Slice access is safe because `headers_end` is checked above
229+
// (`find` gives a valid position, and subtracting 2 can't underflow).
196230
let headers = Headers::try_from(&headers_and_body[..headers_end])?;
197231

198232
// Parse the body of the request.
@@ -203,16 +237,22 @@ impl Request {
203237
None
204238
}
205239
content_length => {
240+
// Multiplication is safe because `CRLF_LEN` is a small constant.
241+
let crlf_end = headers_end
242+
.checked_add(2 * CRLF_LEN)
243+
.ok_or(RequestError::Overflow)?;
244+
// This can't underflow because `headers_and_body.len()` >= `crlf_end`.
245+
let body_len = headers_and_body.len() - crlf_end;
206246
// Headers suggest we have a body, but the buffer is shorter than the specified
207247
// content length.
208-
if headers_and_body.len() - (headers_end + 2 * CRLF_LEN)
209-
< content_length as usize
210-
{
248+
if body_len < content_length as usize {
211249
return Err(RequestError::InvalidRequest);
212250
}
213-
let body_as_bytes = &headers_and_body[(headers_end + 2 * CRLF_LEN)..];
251+
// Slice access is safe because `crlf_end` is the index after two CRLF
252+
// (it is <= `headers_and_body` size).
253+
let body_as_bytes = &headers_and_body[crlf_end..];
214254
// If the actual length of the body is different than the `Content-Length` value
215-
// in the headers then this request is invalid.
255+
// in the headers, then this request is invalid.
216256
if body_as_bytes.len() == content_length as usize {
217257
Some(Body::new(body_as_bytes))
218258
} else {
@@ -341,6 +381,13 @@ mod tests {
341381
expected_request_line
342382
);
343383

384+
// Test for invalid request missing the separator.
385+
let request_line = b"GET";
386+
assert_eq!(
387+
RequestLine::try_from(request_line).unwrap_err(),
388+
RequestError::InvalidRequest
389+
);
390+
344391
// Test for invalid method.
345392
let request_line = b"POST http://localhost/home HTTP/1.0";
346393
assert_eq!(
@@ -366,14 +413,14 @@ mod tests {
366413
let request_line = b"nothing";
367414
assert_eq!(
368415
RequestLine::try_from(request_line).unwrap_err(),
369-
RequestError::InvalidHttpMethod("Unsupported HTTP method.")
416+
RequestError::InvalidRequest
370417
);
371418

372419
// Test for invalid format with no version.
373420
let request_line = b"GET /";
374421
assert_eq!(
375422
RequestLine::try_from(request_line).unwrap_err(),
376-
RequestError::InvalidHttpVersion("Unsupported HTTP version.")
423+
RequestError::InvalidRequest
377424
);
378425
}
379426

@@ -396,6 +443,13 @@ mod tests {
396443
assert_eq!(request.http_version(), Version::Http10);
397444
assert!(request.body.is_none());
398445

446+
// Test for invalid Request (missing CR LF).
447+
let request_bytes = b"GET / HTTP/1.1";
448+
assert_eq!(
449+
Request::try_from(request_bytes).unwrap_err(),
450+
RequestError::InvalidRequest
451+
);
452+
399453
// Test for invalid Request (length is less than minimum).
400454
let request_bytes = b"GET";
401455
assert_eq!(
@@ -440,7 +494,6 @@ mod tests {
440494
// Test for an invalid content length.
441495
let request = Request::try_from(
442496
b"PATCH http://localhost/home HTTP/1.1\r\n\
443-
Expect: 100-continue\r\n\
444497
Content-Length: 5000\r\n\r\nthis is a short body",
445498
)
446499
.unwrap_err();

src/response.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl ResponseHeaders {
109109
let delimitator = b", ";
110110
for (idx, method) in self.allow.iter().enumerate() {
111111
buf.write_all(method.raw())?;
112+
// We check above that `self.allow` is not empty.
112113
if idx < self.allow.len() - 1 {
113114
buf.write_all(delimitator)?;
114115
}

src/server.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ impl<T: Read + Write> ClientConnection<T> {
148148
}
149149
}
150150
}
151-
self.in_flight_response_count += parsed_requests.len() as u32;
151+
self.in_flight_response_count = self
152+
.in_flight_response_count
153+
.checked_add(parsed_requests.len() as u32)
154+
.ok_or(ServerError::Overflow)?;
152155
// If the state of the connection has changed, we need to update
153156
// the event set in the `epoll` structure.
154157
if self.connection.pending_write() {
@@ -180,11 +183,15 @@ impl<T: Read + Write> ClientConnection<T> {
180183
Ok(())
181184
}
182185

183-
fn enqueue_response(&mut self, response: Response) {
186+
fn enqueue_response(&mut self, response: Response) -> Result<()> {
184187
if self.state != ClientConnectionState::Closed {
185188
self.connection.enqueue_response(response);
186189
}
187-
self.in_flight_response_count -= 1;
190+
self.in_flight_response_count = self
191+
.in_flight_response_count
192+
.checked_sub(1)
193+
.ok_or(ServerError::Underflow)?;
194+
Ok(())
188195
}
189196

190197
// Returns `true` if the connection is closed and safe to drop.
@@ -460,6 +467,7 @@ impl HttpServer {
460467
///
461468
/// # Errors
462469
/// `IOError` is returned when an `epoll::ctl` operation fails.
470+
/// `Underflow` is returned when `enqueue_response` fails.
463471
pub fn respond(&mut self, response: ServerResponse) -> Result<()> {
464472
if let Some(client_connection) = self.connections.get_mut(&(response.id as i32)) {
465473
// If the connection was incoming before we enqueue the response, we change its
@@ -468,7 +476,7 @@ impl HttpServer {
468476
client_connection.state = ClientConnectionState::AwaitingOutgoing;
469477
Self::epoll_mod(&self.epoll, response.id as RawFd, epoll::EventSet::OUT)?;
470478
}
471-
client_connection.enqueue_response(response.response);
479+
client_connection.enqueue_response(response.response)?;
472480
}
473481
Ok(())
474482
}

0 commit comments

Comments
 (0)