+
Skip to content

Potentially incorrect normalization code in new_from_ffi #16

@Manishearth

Description

@Manishearth

Caught during unsafe review

uds/src/addr.rs

Lines 647 to 654 in a596894

// normalize addr.len to include terminating NUL byte if possible
// and not be greater than capacity
if addr.len >= capacity {
addr.len = capacity;
} else if addr.addr.sun_path[(addr.len-1-path_offset()) as usize] != 0 {
addr.len += 1;
addr.addr.sun_path[(addr.len-1-path_offset()) as usize] = 0;
}

Some platforms, including FreeBSD, require a null terminator here, which we are sometimes stripping

e.g. FreeBSD:

The sun_path field must be terminated by a NUL character to be used with SUN_LEN(), but the terminating NUL is not part of the address.

We do have some code on OpenBSD that talks about this but it isn't involved here, and it's only OpenBSD, not FreeBSD as well.

uds/src/addr.rs

Lines 252 to 257 in a596894

/// Is the size of the underlying `sun_path` field,
/// minus 1 if the OS is known to require a trailing NUL (`'\0'`) byte.
pub fn max_path_len() -> usize {
mem::size_of_val(&Self::new_unspecified().addr.sun_path)
- if cfg!(target_os="openbsd") {1} else {0}
}

I'd recommend we'd cautiously not strip the NUL except for specific platforms where we know that that's okay.

In general the NUL invariant is also hard to follow in this follow, would be worth documenting it more.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载