Skip to content

fix: prevent null pointer dereference in privilege cleanup#191

Merged
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master
Dec 17, 2025
Merged

fix: prevent null pointer dereference in privilege cleanup#191
robertkill merged 1 commit intolinuxdeepin:masterfrom
robertkill:master

Conversation

@robertkill
Copy link
Contributor

@robertkill robertkill commented Dec 17, 2025

  1. Added null check for m_session pointer before calling deleteLater()
  2. This prevents potential crashes when m_session is null during privilege cleanup
  3. The original code assumed m_session was always valid, which could lead to segmentation faults

Influence:

  1. Test privilege escalation scenarios where authentication might fail
  2. Verify cleanup doesn't crash when session ends unexpectedly
  3. Test multiple privilege requests in sequence
  4. Verify memory management during authentication cancellation

fix: 修复权限清理中的空指针解引用问题

  1. 在调用 deleteLater() 前添加了对 m_session 指针的空值检查
  2. 防止在权限清理过程中 m_session 为空时可能导致的崩溃
  3. 原始代码假设 m_session 始终有效,这可能导致段错误

Influence:

  1. 测试认证可能失败的特权提升场景
  2. 验证会话意外结束时清理过程不会崩溃
  3. 测试连续多次特权请求
  4. 验证认证取消时的内存管理

PMS: BUG-344183

Summary by Sourcery

Bug Fixes:

  • Prevent a potential crash by checking m_session for null before invoking deleteLater() during privilege cleanup.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 17, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Adds a null check around the PolicyKit session pointer before invoking deleteLater() during privilege cleanup to avoid crashes when the session is unexpectedly null.

Sequence diagram for privilege cleanup with null-safe session handling

sequenceDiagram
    actor User
    participant PolicyKitListener
    participant PolkitSession as m_session
    participant Dialog as m_dialog

    User->>PolicyKitListener: finishObtainPrivilege()
    PolicyKitListener->>PolicyKitListener: fillResult()

    alt m_session is not null
        PolicyKitListener->>PolkitSession: deleteLater()
    else m_session is null
        PolicyKitListener->>PolicyKitListener: skip deleteLater()
    end

    PolicyKitListener->>Dialog: close()
    PolicyKitListener->>PolicyKitListener: m_inProgress = false
Loading

Class diagram for PolicyKitListener privilege cleanup changes

classDiagram
    class PolicyKitListener {
        - QSharedPointer m_session
        - Dialog* m_dialog
        - bool m_inProgress
        + finishObtainPrivilege() void
        - fillResult() void
    }

    class QSharedPointer {
        + isNull() bool
        + data() QObject*
    }

    class Dialog {
        + close() void
    }

    class QObject {
        + deleteLater() void
    }

    PolicyKitListener --> QSharedPointer : owns m_session
    PolicyKitListener --> Dialog : uses
    QSharedPointer --> QObject : wraps
    Dialog --|> QObject
Loading

File-Level Changes

Change Details Files
Guard session cleanup against null pointer dereference in privilege completion path.
  • Wrap the m_session deleteLater() call in a null check using m_session.isNull()
  • Ensure privilege cleanup continues safely even when no valid session exists, preventing potential segmentation faults
policykitlistener.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey there - I've reviewed your changes - here's some feedback:

  • After calling deleteLater() on m_session.data(), consider also clearing the QPointer (e.g., m_session.clear()) to make it explicit that the pointer should no longer be used and to avoid accidental reuse elsewhere.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- After calling `deleteLater()` on `m_session.data()`, consider also clearing the `QPointer` (e.g., `m_session.clear()`) to make it explicit that the pointer should no longer be used and to avoid accidental reuse elsewhere.

## Individual Comments

### Comment 1
<location> `policykitlistener.cpp:143` </location>
<code_context>
     // to get cancel state, polkit-qt need be updated
     fillResult();
-    m_session.data()->deleteLater();
+    if (!m_session.isNull()) {
+        m_session.data()->deleteLater();
+    }
</code_context>

<issue_to_address>
**suggestion (bug_risk):** Consider resetting `m_session` after scheduling `deleteLater()` to avoid future misuse.

Given that we now guard `deleteLater()` with a null check, clearing `m_session` right after calling it (e.g. `m_session.clear()`) would make it explicit that the session must not be used again and reduce the risk of subtle use-after-free style issues elsewhere in the code.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

// to get cancel state, polkit-qt need be updated
fillResult();
m_session.data()->deleteLater();
if (!m_session.isNull()) {
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider resetting m_session after scheduling deleteLater() to avoid future misuse.

Given that we now guard deleteLater() with a null check, clearing m_session right after calling it (e.g. m_session.clear()) would make it explicit that the session must not be used again and reduce the risk of subtle use-after-free style issues elsewhere in the code.

18202781743
18202781743 previously approved these changes Dec 17, 2025
1. Added null check for m_session pointer before calling deleteLater()
2. This prevents potential crashes when m_session is null during
privilege cleanup
3. The original code assumed m_session was always valid, which could
lead to segmentation faults

Influence:
1. Test privilege escalation scenarios where authentication might fail
2. Verify cleanup doesn't crash when session ends unexpectedly
3. Test multiple privilege requests in sequence
4. Verify memory management during authentication cancellation

fix: 修复权限清理中的空指针解引用问题

1. 在调用 deleteLater() 前添加了对 m_session 指针的空值检查
2. 防止在权限清理过程中 m_session 为空时可能导致的崩溃
3. 原始代码假设 m_session 始终有效,这可能导致段错误

Influence:
1. 测试认证可能失败的特权提升场景
2. 验证会话意外结束时清理过程不会崩溃
3. 测试连续多次特权请求
4. 验证认证取消时的内存管理

PMS: BUG-344183
@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查:

  1. 语法逻辑:
    修改后的代码在语法上是正确的。增加了对 m_session 的空指针检查,这是一个很好的改进。使用 isNull() 检查比直接判断指针是否为空更安全,因为这是 Qt 框架推荐的方式。

  2. 代码质量:

  • 修改后的代码质量更好,增加了防御性编程,避免了潜在的空指针解引用。
  • 使用了 Qt 的智能指针(QSharedPointer)的 isNull() 方法进行检查,这是 Qt 框架的标准做法。
  1. 代码性能:
  • 性能影响很小。增加的空指针检查只是一个简单的条件判断,不会带来明显的性能开销。
  1. 代码安全:
  • 修改提高了代码的安全性,避免了可能的空指针解引用导致的程序崩溃。
  • 使用 deleteLater() 而不是直接 delete 是一个很好的实践,这确保了对象会在适当的时候被安全删除,特别是在 Qt 的事件循环环境中。

改进建议:

  1. 可以考虑在 m_session 被置空的地方也添加相应的注释,说明生命周期管理。
  2. 考虑在 m_dialog->close() 之前也添加对 m_dialog 的空指针检查,保持一致的防御性编程风格。

总的来说,这是一个很好的改进,提高了代码的健壮性和安全性。建议采纳这个修改。

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: 18202781743, robertkill

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@robertkill robertkill merged commit 2bbb2f2 into linuxdeepin:master Dec 17, 2025
16 of 18 checks passed
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