mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: 라종휘 <jonghwi.rha@samsung.com>
To: Hannes Reinecke <hare@suse.de>, Jens Axboe <axboe@kernel.dk>
Cc: "linux-block@vger.kernel.org" <linux-block@vger.kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"hch@lst.de" <hch@lst.de>, 김정태 <jt1217.kim@samsung.com>,
	정혜연 <hyeon.chung@samsung.com>
Subject: Re: Re: [Patch] bsg: initialize request and reply payloads in bsg_prepare_job
Date: Fri, 06 Feb 2026 13:58:50 +0900	[thread overview]
Message-ID: <20260206045850epcms2p2b10bcfd30a1c768fb35b2994b29454de@epcms2p2> (raw)
In-Reply-To: <dcc9dfc7-fda0-4d25-80ab-48a722c984bf@suse.de>

On 2/6/26 00:45, Hannes Reinecke wrote:
> On 2/5/26 14:42, Jens Axboe wrote:
>> On 2/4/26 10:32 PM, ??? wrote:
>>> bsg: initialize request and reply payloads in bsg_prepare_job
>>>
>>> struct bsg_job payloads contain fields that are only populated by
>>> certain commands, such as sg_list pointers.
>>>
>>> Because struct bsg_job is allocated with kmalloc(), memory may be
>>> reused across requests. If a command does not populate all payload
>>> fields, stale state from a previous job may remain and later be
>>> misinterpreted during cleanup, potentially leading to use-after-free
>>> or double-free issues.
>>>
>>> Initialize both request and reply payloads at the beginning of job
>>> preparation to ensure a clean state for all commands.
>>>
>>> Signed-off-by: Jonghwi Rha <jonghwi.rha@samsung.com>
>>>
>>> diff --git a/block/bsg-lib.c b/block/bsg-lib.c
>>> index 32da4a4429ce..0fbf8e311c03 100644
>>> --- a/block/bsg-lib.c
>>> +++ b/block/bsg-lib.c
>>> @@ -234,6 +234,12 @@ static bool bsg_prepare_job(struct device *dev, struct request *req)
>>>          struct bsg_job *job = blk_mq_rq_to_pdu(req);
>>>          int ret;
>>>
>>> +      /* Clear stale SG state since bsg_job is reused as a request PDU */
>>> +      job->request_payload.sg_list = NULL;
>>> +      job->request_payload.sg_cnt = 0;
>>> +      job->reply_payload.sg_list = NULL;
>>> +      job->reply_payload.sg_cnt = 0;
>>> +
>>>          job->timeout = req->timeout;
>>>
>>>          if (req->bio) {
>>
>> The patch is white-space damaged, tabs are spaces. But I can fix that
>> up. Do we just want to do a memset(job, 0, sizeof(*job)) here to avoid
>> any oddities like this in the future?
>>
>
> That might indeed be better.

The suggested method impairs normal operation. If bsg_prepare_job performs a zero‑memset for the job structure, all request‑related information set on the driver side before the call will be lost. Therefore, if it runs as is, it will go to ufs_bsg_request and cause a null‑pointer access.

Currently, the original patch has no functional impact.

The blank problem seems to be due to a mistake I made while copying and pasting the patch. I am reattaching the patch below. If needed, I can attach the patch and resend the new email.


[PATCH] bsg: initialize request and reply payloads in bsg_prepare_job

struct bsg_job payloads contain fields that are only populated by
certain commands, such as sg_list pointers.

Because struct bsg_job is allocated with kmalloc(), memory may be
reused across requests. If a command does not populate all payload
fields, stale state from a previous job may remain and later be
misinterpreted during cleanup, potentially leading to use-after-free
or double-free issues.

Initialize both request and reply payloads at the beginning of job
preparation to ensure a clean state for all commands.

Signed-off-by: Jonghwi Rha <jonghwi.rha@samsung.com>
---
 block/bsg-lib.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/block/bsg-lib.c b/block/bsg-lib.c
index 32da4a4429ce..0fbf8e311c03 100644
--- a/block/bsg-lib.c
+++ b/block/bsg-lib.c
@@ -234,6 +234,12 @@ static bool bsg_prepare_job(struct device *dev, struct request *req)
 	struct bsg_job *job = blk_mq_rq_to_pdu(req);
 	int ret;
 
+	/* Clear stale SG state since bsg_job is reused as a request PDU */
+	job->request_payload.sg_list = NULL;
+	job->request_payload.sg_cnt = 0;
+	job->reply_payload.sg_list = NULL;
+	job->reply_payload.sg_cnt = 0;
+
 	job->timeout = req->timeout;
 
 	if (req->bio) {
-- 

Regards,
Jonghwi,




      parent reply	other threads:[~2026-02-06  4:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CGME20260130091020epcms2p2d85af8781639a17ab517208feb270dbd@epcms2p4>
2026-02-02 12:04 ` Re: [Samsung] bsg-lib.c patch for double-free error fix 라종휘
2026-02-05  3:46   ` Jens Axboe
     [not found]   ` <CGME20260130091020epcms2p2d85af8781639a17ab517208feb270dbd@epcms2p2>
2026-02-05  5:32     ` [Patch] bsg: initialize request and reply payloads in bsg_prepare_job 라종휘
2026-02-05 13:42       ` Jens Axboe
2026-02-05 23:45         ` Hannes Reinecke
2026-02-06  4:58     ` 라종휘 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260206045850epcms2p2b10bcfd30a1c768fb35b2994b29454de@epcms2p2 \
    --to=jonghwi.rha@samsung.com \
    --cc=axboe@kernel.dk \
    --cc=hare@suse.de \
    --cc=hch@lst.de \
    --cc=hyeon.chung@samsung.com \
    --cc=jt1217.kim@samsung.com \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®