FIX linear.cpp: avoid out-of-bound read in n_iter for crammer_singer - #34273
Merged
Conversation
lorentzenchr
approved these changes
Jun 22, 2026
lorentzenchr
left a comment
Member
There was a problem hiding this comment.
@jakevdp thanks for this fix (I am wondering if upstream liblinear fixed those issues in newer versions)
Member
Author
It's possible: scikit-learn's bundled liblinear was forked from the source 15 years ago! |
Member
|
Do you want to add a whatsnew entry? |
jakevdp
force-pushed
the
linear-oob-write
branch
from
July 13, 2026 16:16
391b4b6 to
8d902dc
Compare
Member
Author
|
I added the whats_new entry – thanks! |
virchan
approved these changes
Jul 14, 2026
prady0t
pushed a commit
to prady0t/scikit-learn
that referenced
this pull request
Sep 2, 2026
jeremiedbb
pushed a commit
to jeremiedbb/scikit-learn
that referenced
this pull request
Sep 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Following up on #34256, I ran some of the SVM tests in ASAN mode and discovered an out-of-bound read in
linear.cpp. Here's a Python script that hits this (run in sklearn v1.9.0, but the issue predates this):The expectation is that
clf.n_iter_would be deterministic, such thatn_iter_valueswould always contain a single entry. But this non-deterministically returns 2 or more entries:ASAN shows that the culprit is an out-of-bound read at line 2949 here:
scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Lines 2940 to 2950 in 88080a5
The issue is the condition in lines 2943-2955: it assumes that the length of
n_iterisnr_class, except in the case thatnr_class == 2. Looking through the file for wheren_iteris allocated, this logic is correct except in the case thatsolver_type == MCSVM_CShere:scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Lines 2531 to 2534 in 88080a5
Here the length of
n_iteris 1, while the associatednr_classis an arbitrary non-negative integer representing the number of classes:scikit-learn/sklearn/svm/src/liblinear/linear.cpp
Line 2493 in 88080a5
The net result of this is that line 2949 is an out-of-bound-read under the following conditions:
LinearSVCwithmulti_class="crammer_singer"The fix here is to specifically check the
solver_typeto use the correct length forn_iterin this read.AI usage disclosure
I used AI to execute tests in ASAN mode and to locate the root cause of this issue. I made the code changes and wrote the PR description manually.