Skip to content

Commit 84ee0b7

Browse files
authored
uapi16: refuse a contents object that refers to the root
A writer takes a contents object whose file is the name of the root and writes {"contents":[{"file":""}],"name":"x"} which a reader refuses: manifest object 1: contents file reference: name is empty: '' Every name is one a reader takes for a file but the root's, which is empty, and ContentsSource::File() can hold it: Name::ROOT, Name::default() and the parent of a name at the top are all the name of the root. So refuse such a contents object in ContentsObject::check(), which checks the values of a contents object as a reader does, with the error a reader gives for the record. That error moves into Error::invalid_file_reference() so that the reader and the writer share it. The message only differs in the name of the object, which a reader doesn't know yet when it reads the contents objects. Since the previous commit set_contents() runs ContentsObject::check() as well, through ContentsObject::check_values(). So it now fills in nothing from such a contents object either. Before, the file "" with the original size 5 gave the object the size 5 and the contents object the encoded size 5, both marked as filled in. The sweep of the previous commit agrees once it has the name of the root among its files. Before this change 15718 of its file objects with one or two contents objects didn't agree with a reader, and the writer took 9741 objects whose records a reader refuses. Now all of them agree, and where a reader takes the record nothing changes. Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
1 parent 8a48c10 commit 84ee0b7

3 files changed

Lines changed: 76 additions & 19 deletions

File tree

‎src/uapi16/decode.rs‎

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,19 +1862,11 @@ impl<'a> Part<'a> for ContentsElement {
18621862
match field {
18631863
Field::File => {
18641864
if let Some(file) = decoder.string(field, value) {
1865-
match Name::new(file.as_ref()) {
1866-
Ok(file) => {
1867-
contents.source = ContentsSource::File(file);
1868-
places += 1;
1869-
}
1870-
Err(reason) => decoder.note_error(Problem::InvalidContents, field, || {
1871-
format!(
1872-
"{}: contents file reference: {}: '{}'",
1873-
subject,
1874-
reason,
1875-
file.escape_debug()
1876-
)
1877-
}),
1865+
let name = Name::new(file.as_ref())
1866+
.map_err(|reason| Error::invalid_file_reference(subject, reason, &file));
1867+
if let Some(name) = decoder.note_err(name) {
1868+
contents.source = ContentsSource::File(name);
1869+
places += 1;
18781870
}
18791871
}
18801872
}
@@ -3101,8 +3093,9 @@ mod tests {
31013093
}
31023094
}
31033095
// A contents object with a value a reader refuses: a URL that is
3104-
// neither http nor https, the encoding that stands for none and a
3105-
// name that is not one of an HTTP header field.
3096+
// neither http nor https, the encoding that stands for none, a name
3097+
// that is not one of an HTTP header field, and the name of the root
3098+
// as the file.
31063099
let ftp = || ContentsObject::new(ContentsSource::Url("ftp://example.com/x".into()));
31073100
let https = || ContentsObject::new(ContentsSource::Url("https://example.com/x".into()));
31083101
for (record, contents, field) in [
@@ -3132,6 +3125,14 @@ mod tests {
31323125
},
31333126
Field::HttpHeaders,
31343127
),
3128+
(
3129+
r#"{"name":"x","contents":[{"file":"","originalSize":5}]}"#,
3130+
ContentsObject {
3131+
original_size: Some(5),
3132+
..ContentsObject::new(ContentsSource::File(Name::ROOT))
3133+
},
3134+
Field::File,
3135+
),
31353136
] {
31363137
let e = refused(record);
31373138
assert_eq!(

‎src/uapi16/error.rs‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::io;
77
use crate::decode::Subject;
88
use crate::object::FileObject;
99
use crate::strict::{ParseError, ParseErrorKind};
10-
use crate::{MAX_CONTENTS, MAX_RECORD_SIZE};
10+
use crate::{NameProblem, MAX_CONTENTS, MAX_RECORD_SIZE};
1111

1212
/// What is wrong with a manifest.
1313
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -637,6 +637,21 @@ impl Error {
637637
.at(Field::Offset)
638638
}
639639

640+
/// A contents object of the file object `subject` names whose `file`
641+
/// is `file`, which is no name of a file for the reason `reason`.
642+
pub(crate) fn invalid_file_reference(subject: Subject<'_>, reason: NameProblem, file: &str) -> Error {
643+
Error::manifest(
644+
Problem::InvalidContents,
645+
format!(
646+
"{}: contents file reference: {}: '{}'",
647+
subject,
648+
reason,
649+
file.escape_debug()
650+
),
651+
)
652+
.at(Field::File)
653+
}
654+
640655
/// `object`, which `subject` names, states the flag of the field
641656
/// `field`, which it cannot have, see [`FileObject::unpermitted_flag`].
642657
pub(crate) fn unpermitted_flag(object: &FileObject, field: Field, subject: Subject<'_>) -> Error {

‎src/uapi16/writer.rs‎

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::object::{
2020
};
2121
use crate::sequence::SequenceCheck;
2222
use crate::{
23-
Features, Name, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END, RECORD_SEPARATOR,
24-
SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE,
23+
Features, Name, NameProblem, Order, Sha256, MAX_CONTENTS, MAX_RECORD_SIZE, MEDIA_TYPE, RECORD_END,
24+
RECORD_SEPARATOR, SIGNATURE_MEDIA_TYPE, STRING_RULES, TRAILER_MEDIA_TYPE, USER_NAMESPACE,
2525
};
2626

2727
/// The most room for a record that an [`Encoder`] keeps from one record to
@@ -729,6 +729,12 @@ impl ContentsObject {
729729
if let Some(encoding) = &self.encoding {
730730
ContentsObject::check_encoding(encoding, subject)?;
731731
}
732+
// Every name is one a reader takes for a file, but the root's: it is
733+
// empty.
734+
if let Some(file) = self.file().filter(|file| file.is_root()) {
735+
let reason = NameProblem::Empty;
736+
return Err(Error::invalid_file_reference(subject, reason, file.as_str()));
737+
}
732738
// A reader checks the header fields in the order they are written.
733739
if !self.http_headers.is_empty() {
734740
ContentsObject::check_headers(self.sorted_headers(), subject)?;
@@ -1785,7 +1791,7 @@ mod tests {
17851791
..GptPartition::default()
17861792
}))
17871793
}
1788-
let cases: [(Give, Problem, Field); 19] = [
1794+
let cases: [(Give, Problem, Field); 20] = [
17891795
(|o| o.inode.uid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Uid),
17901796
(|o| o.inode.gid = Some(u32::MAX), Problem::FieldOutOfRange, Field::Gid),
17911797
// -1 as a 16-bit ID is no ID either.
@@ -1856,6 +1862,11 @@ mod tests {
18561862
Problem::InvalidContents,
18571863
Field::Contents,
18581864
),
1865+
(
1866+
|o| o.contents = vec![ContentsObject::new(ContentsSource::File(Name::ROOT))],
1867+
Problem::InvalidContents,
1868+
Field::File,
1869+
),
18591870
];
18601871
let (root, _) = recording_all();
18611872
for (give, problem, field) in cases {
@@ -1870,6 +1881,36 @@ mod tests {
18701881
object
18711882
);
18721883
}
1884+
// A contents object can't refer to the root. Name::ROOT, the default
1885+
// name and the parent of a name at the top are all the name of the
1886+
// root. A reader refuses the record a writer would write for such an
1887+
// object with the same error, only without the name of the object,
1888+
// which it doesn't know yet.
1889+
for file in [Name::ROOT, Name::default(), name("a").parent().unwrap()] {
1890+
let mut object = FileObject::new(name("x"));
1891+
object.contents = vec![ContentsObject::new(ContentsSource::File(file))];
1892+
let mut writer = Writer::begin(Vec::new(), &root).unwrap();
1893+
let e = writer.file(&object).unwrap_err();
1894+
assert_eq!(
1895+
(e.kind(), e.field(), e.to_string()),
1896+
(
1897+
ErrorKind::Manifest(Problem::InvalidContents),
1898+
Some(Field::File),
1899+
"manifest object 1 (x): contents file reference: name is empty: ''".to_string()
1900+
)
1901+
);
1902+
}
1903+
let (_, root_text) = recording_all();
1904+
let record = r#"{"contents":[{"file":""}],"name":"x"}"#;
1905+
let e = read_whole(&sequence(&[&root_text, record, TRAILER])).unwrap_err();
1906+
assert_eq!(
1907+
(e.kind(), e.field(), e.to_string()),
1908+
(
1909+
ErrorKind::Manifest(Problem::InvalidContents),
1910+
Some(Field::File),
1911+
"manifest object 1: contents file reference: name is empty: ''".to_string()
1912+
)
1913+
);
18731914
// A field of a feature the manifest does not record, also one of
18741915
// those the specification added last.
18751916
let mut object = FileObject::new(name("x"));

0 commit comments

Comments
 (0)