All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Kozub <zub@linux.fjfi.cvut.cz>
To: Scott Bauer <sbauer@plzdonthack.me>
Cc: linux-kernel@vger.kernel.org, axboe@kernel.dk, hch@infradead.org,
	jonathan.derrick@intel.com
Subject: Re: [PATCH v2 11/16] block: sed-opal: ioctl for writing to shadow mbr
Date: Sun, 20 Jan 2019 11:27:30 +0100 (CET)	[thread overview]
Message-ID: <alpine.LRH.2.21.1901201045280.4278@linux.fjfi.cvut.cz> (raw)
In-Reply-To: <20190119171550.GB12171@hacktheplanet>

On Sat, 19 Jan 2019, Scott Bauer wrote:

> On Thu, Jan 17, 2019 at 09:31:51PM +0000, David Kozub wrote:
>
>> +static int write_shadow_mbr(struct opal_dev *dev, void *data)
>> +{
>> +	struct opal_shadow_mbr *shadow = data;
>> +	const u8 __user *src;
>> +	u8 *dst;
>> +	size_t off = 0;
>> +	u64 len;
>> +	int err = 0;
>> +
>> +	/* do the actual transmission(s) */
>> +	src = (u8 *) shadow->data;
>> +	while (off < shadow->size) {
>> +		err = cmd_start(dev, opaluid[OPAL_MBR], opalmethod[OPAL_SET]);
>> +		add_token_u8(&err, dev, OPAL_STARTNAME);
>> +		add_token_u8(&err, dev, OPAL_WHERE);
>> +		add_token_u64(&err, dev, shadow->offset + off);
>> +		add_token_u8(&err, dev, OPAL_ENDNAME);
>> +
>> +		add_token_u8(&err, dev, OPAL_STARTNAME);
>> +		add_token_u8(&err, dev, OPAL_VALUES);
>> +
>> +		/*
>> +		 * The bytestring header is either 1 or 2 bytes, so assume 2.
>> +		 * There also needs to be enough space to accommodate the
>> +		 * trailing OPAL_ENDNAME (1 byte) and tokens added by
>> +		 * cmd_finalize.
>> +		 */
>> +		len = min(remaining_size(dev) - (2+1+CMD_FINALIZE_BYTES_NEEDED),
>> +			  (size_t)(shadow->size - off));
>
> What if remaining_size(dev) <  2 + 1 + CMD_FINALIZE_BYTES_NEEDED? If that's possible we
> get min(UINT_MAX(ish) , some size larger than our remaining buffer) and that's not good.

This is only possible for uselessly small values of IO_BUFFER_LENGTH, 
which is a compile-time value. Originally I thought it's OK as nobody 
would set the value so low. But on a second thought, after reading your 
comment, I think that even if IO_BUFFER_LENGTH was set to such a value, 
the code should fail gracefully.

So I will change it into:

while (off < shadow->size) {
 	/*
 	 * Number of bytes needed in the cmd buffer to terminate the
 	 * write shadow mbr command.
 	 *
 	 * The bytestring header is either 1 or 2 bytes, so assume 2.
 	 * There also needs to be enough space to accommodate the
 	 * trailing OPAL_ENDNAME (1 byte) and tokens added by
 	 * cmd_finalize.
 	 */
 	const size_t write_shadow_mbr_footer_size =
 		2 + 1 + CMD_FINALIZE_BYTES_NEEDED;

 	err = cmd_start(dev, opaluid[OPAL_MBR], opalmethod[OPAL_SET]);
 	add_token_u8(&err, dev, OPAL_STARTNAME);
 	add_token_u8(&err, dev, OPAL_WHERE);
 	add_token_u64(&err, dev, shadow->offset + off);
 	add_token_u8(&err, dev, OPAL_ENDNAME);

 	add_token_u8(&err, dev, OPAL_STARTNAME);
 	add_token_u8(&err, dev, OPAL_VALUES);

 	if (!can_add(&err, dev, write_shadow_mbr_footer_size))
 		break;
 	len = min(remaining_size(dev) - write_shadow_mbr_footer_size,
 		  (size_t)(shadow->size - off));
 	pr_debug("MBR: write bytes %zu+%llu/%llu\n",
 		 off, len, shadow->size);

Please let me know if you would prefer a different solution.

Best regards,
David

  reply	other threads:[~2019-01-20 10:32 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-17 21:31 [PATCH v2 00/16] block: sed-opal: support shadow MBR done flag and write David Kozub
2019-01-17 21:31 ` [PATCH v2 01/16] block: sed-opal: fix typos and formatting David Kozub
2019-01-17 21:31 ` [PATCH v2 02/16] block: sed-opal: use correct macro for method length David Kozub
2019-01-17 21:31 ` [PATCH v2 03/16] block: sed-opal: unify space check in add_token_* David Kozub
2019-01-17 21:31 ` [PATCH v2 04/16] block: sed-opal: close parameter list in cmd_finalize David Kozub
2019-01-17 21:31 ` [PATCH v2 05/16] block: sed-opal: unify cmd start David Kozub
2019-01-17 21:31 ` [PATCH v2 06/16] block: sed-opal: unify error handling of responses David Kozub
2019-01-17 21:31 ` [PATCH v2 07/16] block: sed-opal: reuse response_get_token to decrease code duplication David Kozub
2019-01-17 21:31 ` [PATCH v2 08/16] block: sed-opal: print failed function address David Kozub
2019-01-17 21:31 ` [PATCH v2 09/16] block: sed-opal: split generation of bytestring header and content David Kozub
2019-01-17 21:31 ` [PATCH v2 10/16] block: sed-opal: add ioctl for done-mark of shadow mbr David Kozub
2019-01-17 21:31 ` [PATCH v2 11/16] block: sed-opal: ioctl for writing to " David Kozub
2019-01-19 17:15   ` Scott Bauer
2019-01-20 10:27     ` David Kozub [this message]
2019-01-20 21:20       ` Scott Bauer
2019-01-17 21:31 ` [PATCH v2 12/16] block: sed-opal: unify retrieval of table columns David Kozub
2019-01-17 21:31 ` [PATCH v2 13/16] block: sed-opal: check size of shadow mbr David Kozub
2019-01-19 17:29   ` Scott Bauer
2019-01-17 21:31 ` [PATCH v2 14/16] block: sed-opal: pass steps via argument rather than via opal_dev David Kozub
2019-01-17 21:31 ` [PATCH v2 15/16] block: sed-opal: don't repeat opal_discovery0 in each steps array David Kozub
2019-01-19 17:46   ` Scott Bauer
2019-01-20 20:23     ` David Kozub
2019-01-17 21:31 ` [PATCH v2 16/16] block: sed-opal: rename next to execute_steps David Kozub
2019-01-19 17:53 ` [PATCH v2 00/16] block: sed-opal: support shadow MBR done flag and write Scott Bauer
2019-01-20 20:28   ` David Kozub

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=alpine.LRH.2.21.1901201045280.4278@linux.fjfi.cvut.cz \
    --to=zub@linux.fjfi.cvut.cz \
    --cc=axboe@kernel.dk \
    --cc=hch@infradead.org \
    --cc=jonathan.derrick@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sbauer@plzdonthack.me \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.