Skip to content

SEC tie a loaded sparse matrix to its declared type - #572

Open
adrinjalali wants to merge 2 commits into
skops-dev:mainfrom
adrinjalali:sec/sparsematrix-constructed-type
Open

adrinjalali wants to merge 2 commits into
skops-dev:mainfrom
adrinjalali:sec/sparsematrix-constructed-type

Conversation

@adrinjalali

Copy link
Copy Markdown
Member

Reference Issues/PRs

What does this implement/fix? Explain your changes.

SparseMatrixNode trusted the abstract bases spmatrix and sparray by default, while _construct ignores the declared type and lets load_npz build whatever concrete format the npz member names. A file could declare one of the bases, pass the audit with trusted=None, and still have an unaudited concrete sparse object materialised; or declare any type the caller trusts and get a sparse object instead.

This stops trusting the bases (concrete sparse types were never trusted by default, since the trust check is an exact name match, so genuine files behave as before), refuses a declaration that is not a concrete scipy.sparse type while the file is read, and checks that the built object is a sparse object of the declared class. The class name, not scipy's internal module path, is compared, since that path has moved between scipy versions.

Any other comments?

The index arrays of a trusted sparse file are still not validated (check_format(full_check=True)); that is the same class of issue as #567 and is left for a separate PR.

SparseMatrixNode trusted the abstract spmatrix/sparray bases by default while
load_npz builds whatever concrete format the npz names, so a file could pass
the audit under a base or unrelated type and materialise an unaudited sparse
object. Stop trusting the bases, refuse declarations that are not a concrete
scipy.sparse type, and check the built object against the declared format.
@adrinjalali

Copy link
Copy Markdown
Member Author

@3urek4 would you mind having a look here?

@3urek4 3urek4 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks @adrinjalali, looks good to me. Rejecting non-scipy.sparse.* modules and spmatrix/sparray in __init__ before reading the .npz member, and checking isinstance(obj, sparse_bases) with type(obj).__name__ == self.class_name in _construct, covers both the base-class bypass and the trusted-type spoofing case without breaking across scipy module moves.

Leaving index validation to a separate PR makes sense too (check_format(full_check=True) only exists on csr/csc/bsr, while coo and dia don't have check_format and would need separate bounds checks).

This branch has not been deployed

No deployments
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.

2 participants