Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,12 @@ area.
crashes OBS when the registration is refused; the type data is freed only
when the size holds both fields whole. *All*, pending merge. Issue
[#2195](https://github.com/obs-rust/obs-studio/issues/2195).
- A plugin that registers a source, output, encoder or service with a
structure size that ends partway through a callback or its type data no
longer leaves that field partly copied, which crashed OBS when it was
called later or at shutdown; a field is kept only when the size holds it
whole. *All*, pending merge. Issue
[#2203](https://github.com/obs-rust/obs-studio/issues/2203).

### Security hardening

Expand Down
67 changes: 65 additions & 2 deletions libobs/obs-module.c.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,69 @@ lookup_t *obs_module_load_locale(obs_module_t *module, const char *default_local
return lookup;
}

/* A registration copies only the fields of the caller's structure that lie
* wholly within its size, the rule HAS_VAL() applies to the callbacks. A
* field the size ends inside stays zero instead of keeping some of the
* caller's bytes: a partly copied pointer would be called later, an
* optional callback when OBS needs it and free_type_data at shutdown.
*
* Every field of the four info structures is a pointer, aligned to and as
* large as a pointer, except the 32-bit fields each type lists here. So the
* copy ends at the size rounded down to a pointer, or at the end of a 32-bit
* field that starts there and lies wholly within the size. A size of
* sizeof() is a multiple of a pointer, so such a copy is whole, as before. */
static size_t whole_fields_size(size_t size, const size_t *u32_fields, size_t count)
{
size_t whole = size - size % sizeof(void *);

for (size_t i = 0; i < count; i++) {
if (u32_fields[i] >= whole && u32_fields[i] + sizeof(uint32_t) <= size)
whole = u32_fields[i] + sizeof(uint32_t);
}
return whole;
}

/* The 32-bit fields. */
_Static_assert(sizeof(((struct obs_source_info *)NULL)->type) == sizeof(uint32_t), "obs_source_info.type");
_Static_assert(sizeof(((struct obs_source_info *)NULL)->output_flags) == sizeof(uint32_t),
"obs_source_info.output_flags");
_Static_assert(sizeof(((struct obs_source_info *)NULL)->icon_type) == sizeof(uint32_t), "obs_source_info.icon_type");
_Static_assert(sizeof(((struct obs_source_info *)NULL)->version) == sizeof(uint32_t), "obs_source_info.version");
_Static_assert(sizeof(((struct obs_output_info *)NULL)->flags) == sizeof(uint32_t), "obs_output_info.flags");
_Static_assert(sizeof(((struct obs_encoder_info *)NULL)->type) == sizeof(uint32_t), "obs_encoder_info.type");
_Static_assert(sizeof(((struct obs_encoder_info *)NULL)->caps) == sizeof(uint32_t), "obs_encoder_info.caps");

static size_t copy_size_obs_source_info(size_t size)
{
const size_t u32_fields[] = {
offsetof(struct obs_source_info, type),
offsetof(struct obs_source_info, output_flags),
offsetof(struct obs_source_info, icon_type),
offsetof(struct obs_source_info, version),
};
return whole_fields_size(size, u32_fields, sizeof(u32_fields) / sizeof(u32_fields[0]));
}

static size_t copy_size_obs_output_info(size_t size)
{
const size_t u32_fields[] = {offsetof(struct obs_output_info, flags)};
return whole_fields_size(size, u32_fields, sizeof(u32_fields) / sizeof(u32_fields[0]));
}

static size_t copy_size_obs_encoder_info(size_t size)
{
const size_t u32_fields[] = {
offsetof(struct obs_encoder_info, type),
offsetof(struct obs_encoder_info, caps),
};
return whole_fields_size(size, u32_fields, sizeof(u32_fields) / sizeof(u32_fields[0]));
}

static size_t copy_size_obs_service_info(size_t size)
{
return whole_fields_size(size, NULL, 0);
}

#define REGISTER_OBS_DEF(size_var, structure, dest, info) \
do { \
struct structure data = {0}; \
Expand All @@ -760,7 +823,7 @@ lookup_t *obs_module_load_locale(obs_module_t *module, const char *default_local
goto error; \
} \
\
memcpy(&data, info, size_var); \
memcpy(&data, info, copy_size_##structure(size_var)); \
da_push_back(dest, &data); \
} while (false)

Expand Down Expand Up @@ -824,7 +887,7 @@ void obs_register_source_s(const struct obs_source_info *info, size_t size)
goto error;
}

memcpy(&data, info, size);
memcpy(&data, info, copy_size_obs_source_info(size));

if (data.type == OBS_SOURCE_TYPE_TRANSITION) {
if (data.get_width)
Expand Down
15 changes: 6 additions & 9 deletions rust/obs-core/tests/obs_core/module_source_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! with the same ids, sizes and expected messages. [`Registry::register`]
//! stands for `obs_register_source_s()`: it picks the list with
//! [`list_of`], looks for a duplicate, checks the size with
//! [`size_supported`], copies the part of the structure inside the size,
//! [`size_supported`], copies the fields wholly inside the size,
//! normalizes the flags with [`output_flags`], finds the callbacks inside
//! the size (`HAS_VAL()`), asks [`check`], and names the type with
//! [`versioned_id`], logging what the C logs. The field offsets are those of
Expand Down Expand Up @@ -105,14 +105,11 @@ fn inside(offset: usize, len: usize, size: usize) -> bool {
offset + len <= size
}

/// The bytes of a little-endian `u32` at `offset` that a copy of `size`
/// bytes keeps.
/// A `u32` at `offset` as the copy keeps it: whole inside the size, or
/// zero. The copy ends at the last field wholly within the size, so a field
/// the size cuts is not partly copied (#2203).
fn copied_u32(value: u32, offset: usize, size: usize) -> u32 {
let kept = size.saturating_sub(offset).min(4);
let bytes = value.to_le_bytes();
let mut out = [0u8; 4];
out[..kept].copy_from_slice(&bytes[..kept]);
u32::from_le_bytes(out)
if inside(offset, 4, size) { value } else { 0 }
}

impl Registry {
Expand Down Expand Up @@ -182,7 +179,7 @@ impl Registry {
return Err(());
}

// memcpy(&data, info, size)
// memcpy(&data, info, size), cut down to the last whole field
let data_type = copied_u32(info.source_type, OFF_TYPE, size);
let data_flags = copied_u32(info.output_flags, OFF_OUTPUT_FLAGS, size);
let data_version = copied_u32(info.version, OFF_VERSION, size);
Expand Down
12 changes: 4 additions & 8 deletions rust/obs-core/tests/obs_core/module_source_rules_parity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,11 @@ struct Info {
version: u32,
}

/// The bytes of a little-endian `u32` at `offset` that a copy of `size`
/// bytes keeps.
/// A `u32` at `offset` as the copy keeps it: whole inside the size, or
/// zero. The copy ends at the last field wholly within the size, so a field
/// the size cuts is not partly copied (#2203).
fn copied_u32(value: u32, offset: usize, size: usize) -> u32 {
let kept = size.saturating_sub(offset).min(4);
let mut out = [0u8; 4];
for (o, b) in out.iter_mut().zip(value.to_le_bytes()).take(kept) {
*o = b;
}
u32::from_le_bytes(out)
if offset + 4 <= size { value } else { 0 }
}

/// `HAS_VAL()`: set and whole inside the size.
Expand Down
5 changes: 3 additions & 2 deletions rust/obs-core/tests/obs_core/module_type_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,8 +424,9 @@ fn inside(set: bool, offset: usize, len: usize, size: usize) -> bool {
set && offset + len <= size
}

/// A field as `memcpy(&data, info, size)` leaves it: whole inside the
/// size, or zero. No case here cuts a field.
/// A field as `REGISTER_OBS_DEF()` copies it: whole inside the size, or
/// zero. The copy ends at the last field wholly within the size, so a field
/// the size cuts is not partly copied (#2203).
fn copied<T: Default>(value: T, offset: usize, len: usize, size: usize) -> T {
if offset + len <= size {
value
Expand Down
23 changes: 23 additions & 0 deletions test/cmocka/build/test-graph.json
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,15 @@
],
"requires": "OS_LINUX"
},
"test_register_partial_callbacks": {
"sources": [
"test/cmocka/test_register_partial_callbacks.c"
],
"libraries": [
"OBS::libobs",
"OBS::native-test-cmocka"
]
},
"test_register_refused_type_data": {
"sources": [
"test/cmocka/test_register_refused_type_data.c"
Expand Down Expand Up @@ -4385,6 +4394,20 @@
"test_quick_write": {
"target": "test_quick_write"
},
"test_register_partial_callbacks": {
"target": "test_register_partial_callbacks",
"command": [
"${target}"
],
"optional_prefixes": [
[
"xvfb-run",
"-a",
"-s",
"-screen 0 1280x1024x24 -noreset"
]
]
},
"test_register_refused_type_data": {
"target": "test_register_refused_type_data",
"command": [
Expand Down
Loading
Loading