Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maybe unsound in db_version::next_db_version #1873

Open
lwz23 opened this issue Dec 9, 2024 · 1 comment
Open

Maybe unsound in db_version::next_db_version #1873

lwz23 opened this issue Dec 9, 2024 · 1 comment

Comments

@lwz23
Copy link

lwz23 commented Dec 9, 2024

hello, thank you for your contribution in this project, I am scanning the unsoundness problem in rust project.
I notice the following code:

pub fn next_db_version(
    db: *mut sqlite3,
    ext_data: *mut crsql_ExtData,
    merging_version: Option<i64>,
) -> Result<i64, String> {
    fill_db_version_if_needed(db, ext_data)?;

    let mut ret = unsafe { (*ext_data).dbVersion + 1 };
    if ret < unsafe { (*ext_data).pendingDbVersion } {
        ret = unsafe { (*ext_data).pendingDbVersion };
    }
    if let Some(merging_version) = merging_version {
        if ret < merging_version {
            ret = merging_version;
        }
    }
    unsafe {
        (*ext_data).pendingDbVersion = ret;
    }
    Ok(ret)
}

Considering pub mod db_version in #[cfg(feature = "test")] situation and this is a pub function, I assume user can directly call to this function, if it's this case , I think there may exist a unsound problem in this code, eg. maybe ext_data is null? It will lead to UB. I suggest mark this function as unsafe or add additional check to varify the pointer. I chose to report this issue for security reasons, but don't mind if the function is not intended for external use and should be marked as pub(crate), or if this is an error report and there is actually no unsound problem.

@lwz23
Copy link
Author

lwz23 commented Dec 9, 2024

same for fetch_db_version_from_storage

let schema_changed = if (*ext_data).pDbVersionStmt == ptr::null_mut() {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant