Skip to content

Commit dc1ed74

Browse files
committed
Enforce sandbox for include
1 parent f78f462 commit dc1ed74

File tree

8 files changed

+82
-4
lines changed

8 files changed

+82
-4
lines changed

src/libsyntax/ext/source_util.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use symbol::Symbol;
2121
use tokenstream;
2222
use util::small_vector::SmallVector;
2323

24-
use std::fs::File;
2524
use std::io::prelude::*;
2625
use std::path::PathBuf;
2726
use rustc_data_structures::sync::Lrc;
@@ -99,7 +98,18 @@ pub fn expand_include<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[tokenstream::T
9998
None => return DummyResult::expr(sp),
10099
};
101100
// The file will be added to the code map by the parser
102-
let path = res_rel_file(cx, sp, file);
101+
let path = res_rel_file(cx, sp, file.clone());
102+
let env_sb = cx.parse_sess().env_sandbox();
103+
let path = match env_sb.path_lookup(&path) {
104+
Ok(path) => path,
105+
Err(e) => {
106+
cx.span_err(sp,
107+
&format!("couldn't read {}: {}",
108+
file,
109+
e));
110+
return DummyResult::expr(sp);
111+
}
112+
};
103113
let directory_ownership = DirectoryOwnership::Owned { relative: None };
104114
let p = parse::new_sub_parser_from_file(cx.parse_sess(), &path, directory_ownership, None, sp);
105115

@@ -136,9 +146,10 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::TokenT
136146
Some(f) => f,
137147
None => return DummyResult::expr(sp)
138148
};
149+
let env_sb = cx.parse_sess().env_sandbox();
139150
let file = res_rel_file(cx, sp, file);
140151
let mut bytes = Vec::new();
141-
match File::open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
152+
match env_sb.path_open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
142153
Ok(..) => {}
143154
Err(e) => {
144155
cx.span_err(sp,
@@ -171,9 +182,10 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Toke
171182
Some(f) => f,
172183
None => return DummyResult::expr(sp)
173184
};
185+
let env_sb = cx.parse_sess().env_sandbox();
174186
let file = res_rel_file(cx, sp, file);
175187
let mut bytes = Vec::new();
176-
match File::open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
188+
match env_sb.path_open(&file).and_then(|mut f| f.read_to_end(&mut bytes)) {
177189
Err(e) => {
178190
cx.span_err(sp,
179191
&format!("couldn't read {}: {}", file.display(), e));
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File A
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File B

src/test/compile-fail/sb-inc-limit.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test to see how file sandboxing is working. This blocks all includes.
12+
// compile-flags:--include-prefix {{src-base}}/sb-fixtures/a
13+
14+
fn main() {
15+
let _ = include_str!("sb-fixtures/a/a.in");
16+
let _ = include_str!("sb-fixtures/b/b.in"); //~ERROR: path does not have a valid prefix
17+
}

src/test/compile-fail/sb-inc-none.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test to see how file sandboxing is working. This blocks all includes.
12+
// compile-flags:--clear-include-prefixes
13+
// revisions: include include_str include_bytes
14+
15+
fn main() {
16+
#[cfg(include)]
17+
include!("sb-fixtures/a/a.in");
18+
//[include]~^ERROR path does not have a valid prefix
19+
20+
#[cfg(include_str)]
21+
let _ = include_str!("sb-fixtures/a/a.in");
22+
//[include_str]~^ERROR path does not have a valid prefix
23+
24+
#[cfg(include_bytes)]
25+
let _ = include_bytes!("sb-fixtures/a/a.in");
26+
//[include_bytes]~^ERROR path does not have a valid prefix
27+
}

src/test/run-pass/sb-fixtures/a/a.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File A

src/test/run-pass/sb-fixtures/b/b.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
File B

src/test/run-pass/sb-inc.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test to see how environment sandboxing is working
12+
// compile-flags:--include-prefix {{src-base}}/sb-fixtures/a
13+
// compile-flags:--include-prefix {{src-base}}/sb-fixtures/b/b.in
14+
15+
fn main() {
16+
assert_eq!(include_str!("sb-fixtures/a/a.in"), "File A\n");
17+
assert_eq!(include_str!("sb-fixtures/b/b.in"), "File B\n");
18+
}

0 commit comments

Comments
 (0)