From 0516a692785d800b2b3cc77373f2533b101ce217 Mon Sep 17 00:00:00 2001 From: anonkey <6380129+anonkey@users.noreply.github.com> Date: Tue, 13 Feb 2024 07:57:36 +0100 Subject: [PATCH] fix: unaligned read panic on macos --- .github/workflows/ci.yml | 146 +++++++++++++++++++++------------------ changelog/2311.fixed.md | 1 + src/unistd.rs | 7 +- test/test_unistd.rs | 22 ++++++ 4 files changed, 106 insertions(+), 70 deletions(-) create mode 100644 changelog/2311.fixed.md diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5fe8ae8e8d..af9362dbef 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,16 +10,15 @@ on: types: [checks_requested] permissions: - contents: read + contents: read -env: +env: MSRV: 1.69.0 jobs: - macos: runs-on: macos-13 - env: + env: TARGET: x86_64-apple-darwin steps: - name: checkout @@ -27,43 +26,70 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@master - with: - toolchain: '${{ env.MSRV }}' + with: + toolchain: "${{ env.MSRV }}" components: clippy - name: build uses: ./.github/actions/build with: - TARGET: '${{ env.TARGET }}' + TARGET: "${{ env.TARGET }}" - name: test uses: ./.github/actions/test with: - TARGET: '${{ env.TARGET }}' + TARGET: "${{ env.TARGET }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index + macos-silicon: + runs-on: macos-13-arm64 + env: + TARGET: aarch64-apple-darwin + steps: + - name: checkout + uses: actions/checkout@v4 + + - name: setup Rust + uses: dtolnay/rust-toolchain@master + with: + toolchain: "${{ env.MSRV }}" + components: clippy + + - name: build + uses: ./.github/actions/build + with: + TARGET: "${{ env.TARGET }}" + + - name: test + uses: ./.github/actions/test + with: + TARGET: "${{ env.TARGET }}" + + - name: before_cache_script + run: rm -rf $CARGO_HOME/registry/index # Use cross for QEMU-based testing # cross needs to execute Docker, GitHub Action already has it installed cross: - runs-on: ubuntu-20.04 + runs-on: ubuntu-20.04 needs: [rustfmt, minver, macos, linux_native_builds, rust_stable] strategy: fail-fast: false matrix: - target: [ - arm-unknown-linux-gnueabi, - armv7-unknown-linux-gnueabihf, - i686-unknown-linux-gnu, - i686-unknown-linux-musl, - mips-unknown-linux-gnu, - mips64-unknown-linux-gnuabi64, - mips64el-unknown-linux-gnuabi64, - mipsel-unknown-linux-gnu, - powerpc64le-unknown-linux-gnu, - ] + target: + [ + arm-unknown-linux-gnueabi, + armv7-unknown-linux-gnueabihf, + i686-unknown-linux-gnu, + i686-unknown-linux-musl, + mips-unknown-linux-gnu, + mips64-unknown-linux-gnuabi64, + mips64el-unknown-linux-gnuabi64, + mipsel-unknown-linux-gnu, + powerpc64le-unknown-linux-gnu, + ] steps: - name: checkout @@ -71,8 +97,8 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@master - with: - toolchain: '${{ env.MSRV }}' + with: + toolchain: "${{ env.MSRV }}" components: clippy # cross relies on docker or podman, GitHub Acton already has it installed. @@ -84,14 +110,14 @@ jobs: - name: build uses: ./.github/actions/build with: - TARGET: '${{ matrix.target }}' + TARGET: "${{ matrix.target }}" TOOL: cross RUSTFLAGS: --cfg qemu -D warnings - name: test uses: ./.github/actions/test with: - TARGET: '${{ matrix.target }}' + TARGET: "${{ matrix.target }}" SUDO: "" TOOL: cross RUSTFLAGS: --cfg qemu -D warnings @@ -99,8 +125,6 @@ jobs: - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - - # Tasks for Linux native builds # Only test x86_64 targets on GitHub Action, leave aarch64 one in Cirrus CI. linux_native_builds: @@ -108,10 +132,7 @@ jobs: strategy: fail-fast: false matrix: - target: [ - x86_64-unknown-linux-gnu, - x86_64-unknown-linux-musl, - ] + target: [x86_64-unknown-linux-gnu, x86_64-unknown-linux-musl] steps: - name: checkout @@ -119,8 +140,8 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@master - with: - toolchain: '${{ env.MSRV }}' + with: + toolchain: "${{ env.MSRV }}" components: clippy - name: install targets @@ -129,19 +150,19 @@ jobs: - name: build uses: ./.github/actions/build with: - TARGET: '${{ matrix.TARGET }}' + TARGET: "${{ matrix.TARGET }}" - name: test uses: ./.github/actions/test with: - TARGET: '${{ matrix.TARGET }}' + TARGET: "${{ matrix.TARGET }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index rust_stable: runs-on: ubuntu-20.04 - env: + env: TARGET: x86_64-unknown-linux-gnu steps: - name: checkout @@ -149,24 +170,22 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@stable - with: + with: components: clippy - name: build uses: ./.github/actions/build with: - TARGET: '${{ env.TARGET }}' + TARGET: "${{ env.TARGET }}" - name: test uses: ./.github/actions/test with: - TARGET: '${{ env.TARGET }}' + TARGET: "${{ env.TARGET }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - - # Tasks for cross-compiling, but no testing cross_compiling: runs-on: ubuntu-20.04 @@ -189,8 +208,8 @@ jobs: - target: x86_64-unknown-illumos # Cross claims to support running tests on iOS, but it actually doesn't. # https://github.com/rust-embedded/cross/issues/535 - - target: aarch64-apple-ios - # cargo hack tries to invoke the iphonesimulator SDK for iOS + - target: aarch64-apple-ios + # cargo hack tries to invoke the iphonesimulator SDK for iOS NOHACK: true # Cross claims to support Linux powerpc64, but it really doesn't. # https://github.com/rust-embedded/cross/issues/441 @@ -205,8 +224,8 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@master - with: - toolchain: '${{ env.MSRV }}' + with: + toolchain: "${{ env.MSRV }}" components: clippy - name: install targets @@ -215,18 +234,17 @@ jobs: - name: build uses: ./.github/actions/build with: - TARGET: '${{ matrix.target }}' - BUILD: '${{ env.BUILD }}' - NOHACK: '${{ matrix.NOHACK }}' + TARGET: "${{ matrix.target }}" + BUILD: "${{ env.BUILD }}" + NOHACK: "${{ matrix.NOHACK }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - redox: runs-on: ubuntu-20.04 needs: [rustfmt, minver, macos, linux_native_builds, rust_stable] - env: + env: TARGET: x86_64-unknown-redox CLIPPYFLAGS: -D warnings BUILD: check @@ -237,28 +255,26 @@ jobs: - name: setup Rust # Redox's MSRV policy is unclear. Until they define it, use nightly. uses: dtolnay/rust-toolchain@nightly - with: + with: components: clippy - + - name: install targets run: rustup target add ${{ env.TARGET }} - name: build uses: ./.github/actions/build with: - TARGET: '${{ env.TARGET }}' - BUILD: '${{ env.BUILD }}' - CLIPPYFLAGS: '${{ env.CLIPPYFLAGS }}' + TARGET: "${{ env.TARGET }}" + BUILD: "${{ env.BUILD }}" + CLIPPYFLAGS: "${{ env.CLIPPYFLAGS }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - - # Rust Tier 3 targets can't use Rustup tier3: runs-on: ubuntu-20.04 - env: + env: BUILD: check ZFLAGS: -Zbuild-std CLIPPYFLAGS: -D warnings @@ -281,7 +297,7 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@nightly - with: + with: components: clippy - name: install src @@ -290,21 +306,20 @@ jobs: - name: build uses: ./.github/actions/build with: - TARGET: '${{ matrix.target }}' - BUILD: '${{ env.BUILD }}' - ZFLAGS: '${{ env.ZFLAGS }}' - CLIPPYFLAGS: '${{ env.CLIPPYFLAGS }}' + TARGET: "${{ matrix.target }}" + BUILD: "${{ env.BUILD }}" + ZFLAGS: "${{ env.ZFLAGS }}" + CLIPPYFLAGS: "${{ env.CLIPPYFLAGS }}" - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - # Test that we can build with the lowest version of all dependencies. # "cargo test" doesn't work because some of our dev-dependencies, like # rand, can't build with their own minimal dependencies. minver: runs-on: ubuntu-20.04 - env: + env: TARGET: x86_64-unknown-linux-gnu steps: - name: checkout @@ -313,7 +328,7 @@ jobs: - name: setup Rust uses: dtolnay/rust-toolchain@nightly - - name: setup + - name: setup run: cargo update -Zminimal-versions - name: check @@ -339,4 +354,3 @@ jobs: - name: before_cache_script run: rm -rf $CARGO_HOME/registry/index - diff --git a/changelog/2311.fixed.md b/changelog/2311.fixed.md new file mode 100644 index 0000000000..c1b4013f05 --- /dev/null +++ b/changelog/2311.fixed.md @@ -0,0 +1 @@ +Fixed `::unistd::Group::members` using read_unaligned to avoid crash on misaligned pointers diff --git a/src/unistd.rs b/src/unistd.rs index 3d98a0efe8..ce2d42455a 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -3476,18 +3476,17 @@ impl Group { let mut ret = Vec::new(); for i in 0.. { - let u = unsafe { mem.offset(i) }; - if unsafe { (*u).is_null() } { + let u = unsafe { mem.offset(i).read_unaligned() }; + if u.is_null() { break; } else { - let s = unsafe {CStr::from_ptr(*u).to_string_lossy().into_owned()}; + let s = unsafe {CStr::from_ptr(u).to_string_lossy().into_owned()}; ret.push(s); } } ret } - /// # Safety /// /// If `f` writes to its `*mut *mut libc::group` parameter, then it must diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 56dc59e720..5ec341a7a5 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -1372,3 +1372,25 @@ fn test_eaccess_file_exists() { eaccess(&path, AccessFlags::R_OK | AccessFlags::W_OK) .expect("assertion failed"); } + +#[test] +#[cfg(bsd)] +fn test_group_from() { + let group = Group::from_name("wheel").unwrap().unwrap(); + assert!(group.name == "wheel"); + let group_id = group.gid; + let group = Group::from_gid(group_id).unwrap().unwrap(); + assert_eq!(group.gid, group_id); + assert_eq!(group.name, "wheel"); +} + +#[test] +#[cfg(target_os = "linux")] +fn test_group_from() { + let group = Group::from_name("root").unwrap().unwrap(); + assert!(group.name == "root"); + let group_id = group.gid; + let group = Group::from_gid(group_id).unwrap().unwrap(); + assert_eq!(group.gid, group_id); + assert_eq!(group.name, "root"); +}