libata: reject passthrough WRITE SAME requests
authorChristoph Hellwig <hch@lst.de>
Tue, 25 Apr 2017 11:39:54 +0000 (13:39 +0200)
committerBen Hutchings <ben@decadent.org.uk>
Sat, 26 Aug 2017 01:14:03 +0000 (02:14 +0100)
commit c6ade20f5e50e188d20b711a618b20dd1d50457e upstream.

The WRITE SAME to TRIM translation rewrites the DATA OUT buffer.  While
the SCSI code accomodates for this by passing a read-writable buffer
userspace applications don't cater for this behavior.  In fact it can
be used to rewrite e.g. a readonly file through mmap and should be
considered as a security fix.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
[bwh: Backported to 3.2:
 - Open-code blk_rq_is_passthrough()
 - We don't distinguish which field is invaid so goto invalid_fld
 - Adjust context]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
drivers/ata/libata-scsi.c

index eeadf8f..01a019e 100644 (file)
@@ -3056,6 +3056,14 @@ static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
        if (unlikely(!dev->dma_mode))
                goto invalid_fld;
 
+       /*
+        * We only allow sending this command through the block layer,
+        * as it modifies the DATA OUT buffer, which would corrupt user
+        * memory for SG_IO commands.
+        */
+       if (unlikely(scmd->request->cmd_type != REQ_TYPE_FS))
+               goto invalid_fld;
+
        if (unlikely(scmd->cmd_len < 16))
                goto invalid_fld;
        scsi_16_lba_len(cdb, &block, &n_block);