mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Sasha Levin <Alexander.Levin@microsoft.com>
To: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"stable@vger.kernel.org" <stable@vger.kernel.org>
Cc: Johannes Thumshirn <jthumshirn@suse.de>,
	Hannes Reinecke <hare@suse.com>,
	"Martin K . Petersen" <martin.petersen@oracle.com>,
	Sasha Levin <Alexander.Levin@microsoft.com>
Subject: [PATCH AUTOSEL for 3.18 42/63] scsi: sg: check for valid direction before starting the request
Date: Sat, 3 Mar 2018 22:33:35 +0000	[thread overview]
Message-ID: <20180303223228.27323-42-alexander.levin@microsoft.com> (raw)
In-Reply-To: <20180303223228.27323-1-alexander.levin@microsoft.com>

From: Johannes Thumshirn <jthumshirn@suse.de>

[ Upstream commit 28676d869bbb5257b5f14c0c95ad3af3a7019dd5 ]

Check for a valid direction before starting the request, otherwise we
risk running into an assertion in the scsi midlayer checking for valid
requests.

[mkp: fixed typo]

Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
Link: http://www.spinics.net/lists/linux-scsi/msg104400.html
Reported-by: Dmitry Vyukov <dvyukov@google.com>
Signed-off-by: Hannes Reinecke <hare@suse.com>
Tested-by: Johannes Thumshirn <jthumshirn@suse.de>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Sasha Levin <alexander.levin@microsoft.com>
---
 drivers/scsi/sg.c | 46 ++++++++++++++++++++++++++++++++++------------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/drivers/scsi/sg.c b/drivers/scsi/sg.c
index 783040af3ead..bba2dcd4f324 100644
--- a/drivers/scsi/sg.c
+++ b/drivers/scsi/sg.c
@@ -674,18 +674,14 @@ sg_write(struct file *filp, const char __user *buf, size_t count, loff_t * ppos)
 	 * is a non-zero input_size, so emit a warning.
 	 */
 	if (hp->dxfer_direction == SG_DXFER_TO_FROM_DEV) {
-		static char cmd[TASK_COMM_LEN];
-		if (strcmp(current->comm, cmd)) {
-			printk_ratelimited(KERN_WARNING
-					   "sg_write: data in/out %d/%d bytes "
-					   "for SCSI command 0x%x-- guessing "
-					   "data in;\n   program %s not setting "
-					   "count and/or reply_len properly\n",
-					   old_hdr.reply_len - (int)SZ_SG_HEADER,
-					   input_size, (unsigned int) cmnd[0],
-					   current->comm);
-			strcpy(cmd, current->comm);
-		}
+		printk_ratelimited(KERN_WARNING
+				   "sg_write: data in/out %d/%d bytes "
+				   "for SCSI command 0x%x-- guessing "
+				   "data in;\n   program %s not setting "
+				   "count and/or reply_len properly\n",
+				   old_hdr.reply_len - (int)SZ_SG_HEADER,
+				   input_size, (unsigned int) cmnd[0],
+				   current->comm);
 	}
 	k = sg_common_write(sfp, srp, cmnd, sfp->timeout, blocking);
 	return (k < 0) ? k : count;
@@ -764,6 +760,29 @@ sg_new_write(Sg_fd *sfp, struct file *file, const char __user *buf,
 	return count;
 }
 
+static bool sg_is_valid_dxfer(sg_io_hdr_t *hp)
+{
+	switch (hp->dxfer_direction) {
+	case SG_DXFER_NONE:
+		if (hp->dxferp || hp->dxfer_len > 0)
+			return false;
+		return true;
+	case SG_DXFER_TO_DEV:
+	case SG_DXFER_FROM_DEV:
+	case SG_DXFER_TO_FROM_DEV:
+		if (!hp->dxferp || hp->dxfer_len == 0)
+			return false;
+		return true;
+	case SG_DXFER_UNKNOWN:
+		if ((!hp->dxferp && hp->dxfer_len) ||
+		    (hp->dxferp && hp->dxfer_len == 0))
+			return false;
+		return true;
+	default:
+		return false;
+	}
+}
+
 static int
 sg_common_write(Sg_fd * sfp, Sg_request * srp,
 		unsigned char *cmnd, int timeout, int blocking)
@@ -784,6 +803,9 @@ sg_common_write(Sg_fd * sfp, Sg_request * srp,
 			"sg_common_write:  scsi opcode=0x%02x, cmd_size=%d\n",
 			(int) cmnd[0], (int) hp->cmd_len));
 
+	if (!sg_is_valid_dxfer(hp))
+		return -EINVAL;
+
 	k = sg_start_req(srp, cmnd);
 	if (k) {
 		SCSI_LOG_TIMEOUT(1, sg_printk(KERN_INFO, sfp->parentdp,
-- 
2.14.1

  parent reply	other threads:[~2018-03-03 22:41 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-03 22:33 [PATCH AUTOSEL for 3.18 01/63] Input: tsc2007 - check for presence and power down tsc2007 during probe Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 02/63] kretprobes: Ensure probe location is at function entry Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 03/63] HID: reject input outside logical range only if null state is set Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 04/63] net: mvpp2: set dma mask and coherent dma mask on PPv2.2 Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 05/63] PCI/MSI: Stop disabling MSI/MSI-X in pci_device_shutdown() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 07/63] perf tools: Make perf_event__synthesize_mmap_events() scale Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 06/63] selinux: check for address length in selinux_socket_bind() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 08/63] drivers: net: xgene: Fix hardware checksum setting Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 09/63] drm: Defer disabling the vblank IRQ until the next interrupt (for instant-off) Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 10/63] ath10k: disallow DFS simulation if DFS channel is not enabled Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 11/63] HID: clamp input to logical range if no null state Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 12/63] ARM: dts: Adjust moxart IRQ controller and flags Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 13/63] batman-adv: handle race condition for claims between gateways Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 14/63] of: fix of_device_get_modalias returned length when truncating buffers Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 15/63] scsi: ipr: Fix missed EH wakeup Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 16/63] [media] media: i2c/soc_camera: fix ov6650 sensor getting wrong clock Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 18/63] sched: act_csum: don't mangle TCP and UDP GSO packets Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 17/63] timers, sched_clock: Update timeout for clock wrap Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 19/63] spi: omap2-mcspi: poll OMAP2_MCSPI_CHSTAT_RXS for PIO transfer Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 20/63] tcp: sysctl: Fix a race to avoid unexpected 0 window from space Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 22/63] mm: Fix false-positive VM_BUG_ON() in page_cache_{get,add}_speculative() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 21/63] powerpc/xmon: Fix an unexpected xmon on/off state change Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 23/63] blk-throttle: make sure expire time isn't too big Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 25/63] braille-console: Fix value returned by _braille_console_setup Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 24/63] ARM: DRA7: hwmod_data: Prevent wait_target_disable error for usb_otg_ss Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 26/63] ARM: dts: r8a7790: Correct parent of SSI[0-9] clocks Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 28/63] powerpc: Avoid taking a data miss on every userspace instruction miss Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 27/63] ARM: dts: r8a7791: Correct parent of SSI[0-9] clocks Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 29/63] net/faraday: Add missing include of of.h Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 30/63] reiserfs: Make cancel_old_flush() reliable Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 31/63] fm10k: correctly check if interface is removed Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 33/63] iommu/iova: Fix underflow bug in __alloc_and_insert_iova_range Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 32/63] apparmor: Make path_max parameter readonly Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 34/63] ARM: dts: rockchip: disable arm-global-timer for rk3188 Sasha Levin
2018-03-04 23:20   ` Alexander Kochetkov
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 35/63] video: ARM CLCD: fix dma allocation size Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 36/63] drm/radeon: Fail fb creation from imported dma-bufs Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 37/63] MIPS: BPF: Quit clobbering callee saved registers in JIT code Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 38/63] regulator: isl9305: fix array size Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 39/63] usb: gadget: dummy_hcd: Fix wrong power status bit clear/reset in dummy_hub_control() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 40/63] perf inject: Copy events when reordering events in pipe mode Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 41/63] perf session: Don't rely on evlist " Sasha Levin
2018-03-03 22:33 ` Sasha Levin [this message]
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 43/63] scsi: sg: close race condition in sg_remove_sfp_usercontext() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 44/63] kprobes/x86: Fix kprobe-booster not to boost far call instructions Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 46/63] wil6210: fix memory access violation in wil_memcpy_from/toio_32 Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 45/63] kprobes/x86: Set kprobes pages read-only Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 47/63] HID: elo: clear BTN_LEFT mapping Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 48/63] sched: Stop resched_cpu() from sending IPIs to offline CPUs Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 50/63] mtd: nand: fix interpretation of NAND_CMD_NONE in nand_command[_lp]() Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 49/63] net: xfrm: allow clearing socket xfrm policies Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 51/63] ARM: dts: am335x-pepper: Fix the audio CODEC's reset pin Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 52/63] ARM: dts: omap3-n900: " Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 54/63] ASoC: tlv320aic31xx: Handle inverted BCLK in non-DSP modes Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 53/63] mtd: nand: ifc: update bufnum mask for ver >= 2.0.0 Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 55/63] tools/usbip: fixes build with musl libc toolchain Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 57/63] scsi: devinfo: apply to HP XP the same flags as Hitachi VSP Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 56/63] spi: sun6i: disable/unprepare clocks on remove Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 58/63] media: cpia2: Fix a couple off by one bugs Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 59/63] veth: set peer GSO values Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 60/63] mac80211: remove BUG() when interface type is invalid Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 61/63] ASoC: nuc900: Fix a loop timeout test Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 62/63] rcutorture/configinit: Fix build directory error message Sasha Levin
2018-03-03 22:33 ` [PATCH AUTOSEL for 3.18 63/63] ima: relax requiring a file signature for new files with zero length Sasha Levin

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=20180303223228.27323-42-alexander.levin@microsoft.com \
    --to=alexander.levin@microsoft.com \
    --cc=hare@suse.com \
    --cc=jthumshirn@suse.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    --cc=stable@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®