Skip to content

Bug: FindVolumeHandle::is_invalid() does not check for a value of -1 #1630

Closed

Description

@BitsE9

Which crate is this about?

windows

Crate version

0.34.0

Summary

FindVolumeHandle::is_invalid() only considers the value 0 to represent an invalid handle which leads to incorrect branches being followed if a function returns a handle value of -1 as its invalid handle code.

Toolchain version/configuration

Default host: x86_64-pc-windows-msvc
rustup home: C:\Users\Ian.rustup

installed toolchains

stable-x86_64-pc-windows-msvc
nightly-2022-01-27-x86_64-pc-windows-msvc
nightly-x86_64-pc-windows-msvc (default)

installed targets for active toolchain

i686-pc-windows-msvc
x86_64-pc-windows-msvc

active toolchain

nightly-x86_64-pc-windows-msvc (default)
rustc 1.61.0-nightly (d3ad51b48 2022-02-25)

Reproducible example

use windows::Win32::Storage::FileSystem::{FindFirstVolumeW, FindVolumeClose};

fn main() {
    unsafe {
        // will return `-1` due to empty output buffer.
        let handle = FindFirstVolumeW(&mut []);
        if !handle.is_invalid() {
            // heap corruption due to handle being `-1`
            FindVolumeClose(handle);
        }
    }
}

Crate manifest

[package]
name = "find_volume_bug"
version = "0.1.0"
edition = "2021"

[target.'cfg(windows)'.dependencies]
windows = { version = "0.34.0", features = ["Win32_Storage_FileSystem", "Win32_Foundation"] }

Expected behavior

Body of if block goes unentered because the handle is invalid and the program exits gracefully.

Actual behavior

Body of if block gets entered because the is_invalid fails to catch the value -1 and the program exits due to heap corruption.

Additional comments

No response

Activity

  1. riverar commented on Mar 24, 2022

    @riverar
    Collaborator

    Thanks @IanE9.

    I believe this is related to microsoft/win32metadata#25. That is, this API has metadata describing a specialized handle return type. So because it's not a windows::Win32::Foundation::HANDLE, the -1 check doesn't happen. (Some APIs use -1, null, 666, etc. as invalid handle values.) I think we need to consider bumping up priority of this issue on the metadata side.

    Will double-check with Kenny Kerr (@kennykerr) before transferring.

  2. kennykerr commented on Mar 24, 2022

    @kennykerr
    Collaborator

    Unfortunately, there's no metadata describing what constitutes an invalid handle. Right now is_invalid for most handles just assumes that 0 is invalid:

    pub fn is_invalid(&self) -> bool {
    *self == unsafe { ::core::mem::zeroed() }
    }

    Unless the metadata is added, we could just assume that -1 may also be an invalid value as HANDLE currently does:

    pub fn is_invalid(&self) -> bool {
    self.0 == 0 || self.0 == -1
    }

    Or we could just get out of the business of providing an is_invalid function altogether and leave that up to the caller to figure out.

    There aren't all that many handle types, so perhaps adding the metadata would be the best route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions