Skip to content

squid: mgr/PyFormatter: prevent segfault on non-printable characters - #71565

Merged
batrick merged 1 commit into
ceph:squidfrom
NitzanMordhai:wip-80036-squid
Oct 20, 2026
Merged

batrick merged 1 commit into
ceph:squidfrom
NitzanMordhai:wip-80036-squid

Conversation

@NitzanMordhai

Copy link
Copy Markdown
Contributor

@NitzanMordhai
NitzanMordhai requested a review from a team as a code owner September 6, 2026 06:58
@NitzanMordhai NitzanMordhai added this to the squid milestone Sep 6, 2026
@github-actions github-actions Bot added the mgr label Sep 6, 2026
github-actions[bot]

This comment was marked as outdated.

@github-actions github-actions Bot added the releng-audit-fail Release engineering: failed backport verification audit. label Sep 6, 2026
@SrinivasaBharath

Copy link
Copy Markdown
Contributor

This PR has been added to wip-bharath11-testing-20260908.043338-squid.

@badone

badone commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@NitzanMordhai maybe a word about why you dropped the test in the Squid backport may placate the release engineering audit gods :P

@NitzanMordhai

Copy link
Copy Markdown
Contributor Author

@NitzanMordhai maybe a word about why you dropped the test in the Squid backport may placate the release engineering audit gods :P

Yes, sorry. NitzanMordhai@535ae6d never backported. The changes are not in squid. And it is based on other functions that were added in that related PR #66432

If PyFormatter::dump_string attempting to format non-printable characters
can result in a nullptr being passed to dump_pyobject, causing a segfault.

It occured in the following case - crash module tried to load the crash entries
containing matedata with non-printable characters.
when these were passed to PyFormatter, the resulting crash prevented ceph-mgr
from starting.

Fix this by attempting to create a python object using PyUnicode_FromString.
If it returns nullptr (indicating a decoding failure), the code now falls
back to decoding using Latin1.
Additionally, add a guard in dump_pyobject to prevent processing nullptr objects,
providing a secondary layer of protection against segfaults.

Fixes: https://tracker.ceph.com/issues/74379
Signed-off-by: Nitzan Mordechai <[email protected]>
(cherry picked from commit 7c68a00)

Conflicts:
  - src/mgr/PyFormatter.h
    Original: 7c68a00#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
    Backport: 30b1e3a#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
      - TTL Cache was never backported to squid, change remove "protected"

  - src/test/mgr/test_pyformatter.cc
    Original: 7c68a00#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
    Backport: 30b1e3a#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
     Test suite didn't backported to squid, delete the file
@NitzanMordhai NitzanMordhai removed the releng-audit-fail Release engineering: failed backport verification audit. label Sep 8, 2026
@NitzanMordhai

Copy link
Copy Markdown
Contributor Author

/audit retest

github-actions[bot]

This comment was marked as outdated.

@github-actions github-actions Bot added the releng-audit-fail Release engineering: failed backport verification audit. label Sep 8, 2026
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown

🔄 Audit Retest Triggered by @NitzanMordhai.

View workflow run

@github-actions github-actions Bot removed the releng-audit-fail Release engineering: failed backport verification audit. label Sep 8, 2026

@github-actions github-actions Bot 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.

Ceph Release Engineering Audit Report

Commit Parity Visualizer

BACKPORT PR #71565 SOURCE PR SOURCE STATUS
dfbb4a9 mgr/PyFormatter: prevent segfault on non-printable characters PR #67022 7c68a00 mgr/PyFormatter: prevent segfault on non-printable characters

Automated Backport Parity Review - Cherry-Pick Conflicts / Deviations

⚠️ WARNING: Cherry-pick conflicts or deviations were found that require component lead review.

A conflict or deviation was detected during the simulation of this backport. The code in this PR does not match a clean cherry-pick of the upstream commits.

This does not necessarily indicate an issue but a maintainer should review.

**Click to expand conflict summaries.**

Deviation in Backport dfbb4a9 (cherry-pick of 7c68a00)

Affected File(s)

Range Diff

Click to expand
--- Original (7c68a00c)
+++ Backport (dfbb4a9b)
@@ -16,3 +16,15 @@
 
 Fixes: https://tracker.ceph.com/issues/74379
 Signed-off-by: Nitzan Mordechai <[email protected]>
+(cherry picked from commit 7c68a00cce5b7769c2e34fb7611d14825283fcda)
+
+Conflicts:
+  - src/mgr/PyFormatter.h
+    Original: 7c68a00#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
+    Backport: 30b1e3a#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
+      - TTL Cache was never backported to squid, change remove "protected"
+
+  - src/test/mgr/test_pyformatter.cc
+    Original: 7c68a00#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
+    Backport: 30b1e3a#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
+     Test suite didn't backported to squid, delete the file

================================================================================
RANGE DIFF
================================================================================

1:  7c68a00cce5 ! 1:  dfbb4a9b0d0 mgr/PyFormatter: prevent segfault on non-printable characters
    @@ Commit message
     
         Fixes: https://tracker.ceph.com/issues/74379
         Signed-off-by: Nitzan Mordechai <[email protected]>
    +    (cherry picked from commit 7c68a00cce5b7769c2e34fb7611d14825283fcda)
    +
    +    Conflicts:
    +      - src/mgr/PyFormatter.h
    +        Original: 7c68a00#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
    +        Backport: 30b1e3a#diff-ac7d387d13545f28de056e1a55bc5f73ec7b3e1bafd077355a3a2349b741489b
    +          - TTL Cache was never backported to squid, change remove "protected"
    +
    +      - src/test/mgr/test_pyformatter.cc
    +        Original: 7c68a00#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
    +        Backport: 30b1e3a#diff-d5583b1cec8167f6ad854612d3c99940b73b8c2a4e61008a1699dc2fb86bbc4c
    +         Test suite didn't backported to squid, delete the file
     
      ## src/mgr/ActivePyModules.cc ##
     @@ src/mgr/ActivePyModules.cc: PyObject *ActivePyModules::get_store_prefix(const std::string &module_name,
    @@ src/mgr/PyFormatter.h: public:
      
     +  void dump_pyobject(std::string_view name, PyObject *p);
     +
    - protected:
    + private:
        PyObject *root;
        PyObject *cursor;
        std::stack<PyObject *> stack;
    + 
     -  void dump_pyobject(std::string_view name, PyObject *p);
    - private:
    +-
        class PendingStream {
          public:
    -
    - ## src/test/mgr/test_pyformatter.cc ##
    -@@ src/test/mgr/test_pyformatter.cc: TEST(PyFormatter, EmptySectionName)
    - 
    - /* End Negative Tests */
    - 
    -+TEST(PyFormatter, DumpPyObject_StoresPrebuiltObject)
    -+{
    -+  // Verify that dump_pyobject inserts a caller-owned PyObject into the dict
    -+  // without re-encoding, which is the pattern used in get_store_prefix.
    -+  PyFormatter py_f;
    -+  PyObject *value = PyUnicode_FromString("hello");
    -+  ASSERT_NE(value, nullptr);
    -+  py_f.dump_pyobject("key", value);
    -+
    -+  PyObject *result = py_f.get();
    -+  ASSERT_NE(result, nullptr);
    -+  ASSERT_TRUE(PyDict_Check(result));
    -+
    -+  PyObject *item = PyDict_GetItemString(result, "key");
    -+  ASSERT_NE(item, nullptr);
    -+  ASSERT_TRUE(PyUnicode_Check(item));
    -+  ASSERT_STREQ(PyUnicode_AsUTF8(item), "hello");
    -+
    -+  Py_DECREF(result);
    -+}
    -+
    -+TEST(PyFormatter, DumpString_SkipsNonUtf8Value)
    -+{
    -+  // dump_string uses PyUnicode_FromStringAndSize which fails on invalid UTF-8.
    -+  // The method must not crash and must simply skip the key.
    -+  PyFormatter py_f;
    -+  // "\x80\xff" is invalid UTF-8
    -+  std::string bad_utf8("\x80\xff", 2);
    -+  py_f.dump_string("bad_key", bad_utf8);
    -+
    -+  PyObject *result = py_f.get();
    -+  ASSERT_NE(result, nullptr);
    -+  ASSERT_TRUE(PyDict_Check(result));
    -+
    -+  // Key must be absent — dump_string skips on encoding failure
    -+  PyObject *item = PyDict_GetItemString(result, "bad_key");
    -+  ASSERT_EQ(item, nullptr);
    -+
    -+  Py_DECREF(result);
    -+}
    -+
    -+TEST(PyFormatter, DumpPyObject_NonUtf8ViLatin1Fallback)
    -+{
    -+  // This is the pattern used in get_store_prefix to handle crash metadata
    -+  // containing non-printable / non-UTF-8 bytes: try UTF-8, fall back to
    -+  // Latin-1, then call dump_pyobject directly with the validated PyObject*.
    -+  PyFormatter py_f;
    -+  std::string bad_utf8("\x80\xff", 2);
    -+
    -+  PyObject *value = PyUnicode_FromStringAndSize(bad_utf8.c_str(), bad_utf8.size());
    -+  if (!value) {
    -+    PyErr_Clear();
    -+    value = PyUnicode_DecodeLatin1(bad_utf8.c_str(), bad_utf8.size(), nullptr);
    -+  }
    -+  ASSERT_NE(value, nullptr);  // Latin-1 must always succeed
    -+  py_f.dump_pyobject("latin1_key", value);
    -+
    -+  PyObject *result = py_f.get();
    -+  ASSERT_NE(result, nullptr);
    -+  ASSERT_TRUE(PyDict_Check(result));
    -+
    -+  // Key must be present — Latin-1 fallback succeeded
    -+  PyObject *item = PyDict_GetItemString(result, "latin1_key");
    -+  ASSERT_NE(item, nullptr);
    -+  ASSERT_TRUE(PyUnicode_Check(item));
    -+
    -+  Py_DECREF(result);
    -+}
    -+
    -+
    - /*
    -  * Tests for PyFormatterRO (read-only-on-the-fly builder):
    -  *  - lists -> tuples, sets -> frozensets
    +     PyObject *cursor;

How to proceed:

  • Authors (Genuine Conflicts): If this is a genuine conflict requiring manual resolution, ensure your resolution is correct. You must explain the conflict resolution in the commit message (e.g., leave the standard Git Conflicts: block intact) and include an explanation for changes.
  • Authors (Need Help?): Reach out to the Component Lead for technical guidance on complex code conflicts.
  • Component Leads (Review): Please review the Range Diff(s) above to verify the author's manual conflict resolution is correct for this release branch. If the deviation is intentional, documented, and approved then the component lead or @ceph/ceph-release-manager can bypass this check by commenting /audit override.

Be familiar with the rules and guidelines for writing backports.


🛟 Need Help?

If you need technical help resolving these issues, please consult with the Component Lead. If you need administrative overrides, please see the #ceph-upstream-releases channel on Slack and request a review from the @ceph/ceph-release-manager.

📋 Component Lead / Release Manager

To override the audit failure, apply releng-audit-override label or comment /audit override.


⚠️ Note: Automated audit checks will be suspended on future pushes to prevent comment spam while you work.

When you are ready for a new audit, please remove the releng-audit-fail label or comment /audit retest.

View workflow run

@github-actions github-actions Bot added the releng-audit-fail Release engineering: failed backport verification audit. label Sep 8, 2026
@ljflores

ljflores commented Oct 8, 2026

Copy link
Copy Markdown
Member

@github-actions github-actions Bot removed the releng-audit-fail Release engineering: failed backport verification audit. label Oct 8, 2026
@github-actions

github-actions Bot commented Oct 8, 2026

Copy link
Copy Markdown

✅ Audit Override Applied by @ljflores.

View workflow run

@ljflores

ljflores commented Oct 8, 2026

Copy link
Copy Markdown
Member

@NitzanMordhai maybe a word about why you dropped the test in the Squid backport may placate the release engineering audit gods :P

Yes, sorry. NitzanMordhai@535ae6d never backported. The changes are not in squid. And it is based on other functions that were added in that related PR #66432

Overrode the audit based on this comment.

@batrick batrick modified the milestones: squid, v19.2.8 Oct 20, 2026
@batrick
batrick merged commit b0f35a7 into ceph:squid Oct 20, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants