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

Fix possible NULL pointer dereference. #858

Merged
merged 1 commit into from
Sep 4, 2024

Conversation

vitek
Copy link
Contributor

@vitek vitek commented Mar 16, 2024

This caused SEGFAULT when moving sql3 db from 1.7 to 1.9. With this fix I've managed to use old db successfully.

Copy link

codecov bot commented Mar 16, 2024

Codecov Report

Attention: Patch coverage is 0% with 5 lines in your changes are missing coverage. Please review.

Project coverage is 72.41%. Comparing base (1b3aab9) to head (c8bb90e).
Report is 1 commits behind head on master.

Files Patch % Lines
src/lib/db.c 0.00% 5 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #858      +/-   ##
==========================================
- Coverage   72.43%   72.41%   -0.02%     
==========================================
  Files          34       34              
  Lines        9773     9775       +2     
==========================================
  Hits         7079     7079              
- Misses       2694     2696       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

src/lib/db.c Outdated
@@ -2169,7 +2169,7 @@ static CK_RV dbup_handler_from_7_to_8(sqlite3 *updb) {

/* for each tobject */
CK_ATTRIBUTE_PTR a = attr_get_attribute_by_type(tobj->attrs, CKA_ALLOWED_MECHANISMS);
CK_BYTE type = type_from_ptr(a->pValue, a->ulValueLen);
CK_BYTE type = a ? type_from_ptr(a->pValue, a->ulValueLen): 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the object doesn't have the attribute CKA_ALLOWED_MECHANISMS, it should just be skipped. This patch would cause us to process everything no matter what, which is harmless, but not needed.

if (a) {
    CK_BYTE type = type_from_ptr(a->pValue, a->ulValueLen);
    if (type != TYPE_BYTE_INT_SEQ) {
        rv = _db_update_tobject_attrs(updb, tobj->id, tobj->attrs);
    }
}
tobject_free(tobj);
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean something like this?

if (a) {
    CK_BYTE type = type_from_ptr(a->pValue, a->ulValueLen);
    if (type != TYPE_BYTE_INT_SEQ) {
        rv = _db_update_tobject_attrs(updb, tobj->id, tobj->attrs);
    }
} else {
   tobject_free(tobj);
   continue;
}

or let it fail?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for an else clause, as we need it to perform the tobject_free() or a memory leak will ensue. We also need to step the database so we can get the next row item to check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we shouldn't check rv in this case, right?

        if (a && rv != CKR_OK) {
            goto error;
        }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, you could also just unconditionally set rv = CKR_OK before the statement, either way is fine by me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed

Copy link
Member

@williamcroberts williamcroberts left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So overall the diff is fine, but you need to squash your two commits into one and improve the commit message and add Fixes: #845 and don't forget to signoff on your commit. Thanks!

Fixes: tpm2-software#845

Signed-off-by: Victor Makarov <vitja.makarov@gmail.com>
@vitek
Copy link
Contributor Author

vitek commented Mar 26, 2024

So overall the diff is fine, but you need to squash your two commits into one and improve the commit message and add Fixes: #845 and don't forget to signoff on your commit. Thanks!

So overall the diff is fine, but you need to squash your two commits into one and improve the commit message and add Fixes: #845 and don't forget to signoff on your commit. Thanks!

done

@sergiorussia
Copy link

@williamcroberts could u take a look? the fix is critical

@sergiorussia
Copy link

sergiorussia commented Sep 4, 2024

also tagging some other active maintainers (https://github.com/orgs/tpm2-software/people) cuz last William's activity was on August 11.

@AndreasFuchsTPM
@pdxjohnny
@JuergenReppSIT
@wxleong

guys could you take a look?
the bug prevents eg from upgrading to Ubuntu 24.04 LTS

ps: after merging this one we could reject #849 as a duplicate

@williamcroberts williamcroberts merged commit 18989a3 into tpm2-software:master Sep 4, 2024
13 checks passed
@sergiorussia
Copy link

@williamcroberts could you please also give any ETA for next release with the fix?

@williamcroberts
Copy link
Member

williamcroberts commented Sep 4, 2024 via email

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

Successfully merging this pull request may close these issues.

3 participants