88#include <linux/io_uring.h>
99#include "nvme.h"
1010
11+ enum {
12+ NVME_IOCTL_VEC = (1 << 0 ),
13+ NVME_IOCTL_PARTITION = (1 << 1 ),
14+ };
15+
1116static bool nvme_cmd_allowed (struct nvme_ns * ns , struct nvme_command * c ,
12- fmode_t mode )
17+ unsigned int flags , fmode_t mode )
1318{
1419 u32 effects ;
1520
1621 if (capable (CAP_SYS_ADMIN ))
1722 return true;
1823
24+ /*
25+ * Do not allow unprivileged passthrough on partitions, as that allows an
26+ * escape from the containment of the partition.
27+ */
28+ if (flags & NVME_IOCTL_PARTITION )
29+ return false;
30+
1931 /*
2032 * Do not allow unprivileged processes to send vendor specific or fabrics
2133 * commands as we can't be sure about their effects.
@@ -150,7 +162,7 @@ static struct request *nvme_alloc_user_request(struct request_queue *q,
150162static int nvme_map_user_request (struct request * req , u64 ubuffer ,
151163 unsigned bufflen , void __user * meta_buffer , unsigned meta_len ,
152164 u32 meta_seed , void * * metap , struct io_uring_cmd * ioucmd ,
153- bool vec )
165+ unsigned int flags )
154166{
155167 struct request_queue * q = req -> q ;
156168 struct nvme_ns * ns = q -> queuedata ;
@@ -163,7 +175,7 @@ static int nvme_map_user_request(struct request *req, u64 ubuffer,
163175 struct iov_iter iter ;
164176
165177 /* fixedbufs is only for non-vectored io */
166- if (WARN_ON_ONCE (vec ))
178+ if (WARN_ON_ONCE (flags & NVME_IOCTL_VEC ))
167179 return - EINVAL ;
168180 ret = io_uring_cmd_import_fixed (ubuffer , bufflen ,
169181 rq_data_dir (req ), & iter , ioucmd );
@@ -172,8 +184,8 @@ static int nvme_map_user_request(struct request *req, u64 ubuffer,
172184 ret = blk_rq_map_user_iov (q , req , NULL , & iter , GFP_KERNEL );
173185 } else {
174186 ret = blk_rq_map_user_io (req , NULL , nvme_to_user_ptr (ubuffer ),
175- bufflen , GFP_KERNEL , vec , 0 , 0 ,
176- rq_data_dir (req ));
187+ bufflen , GFP_KERNEL , flags & NVME_IOCTL_VEC , 0 ,
188+ 0 , rq_data_dir (req ));
177189 }
178190
179191 if (ret )
@@ -203,9 +215,9 @@ static int nvme_map_user_request(struct request *req, u64 ubuffer,
203215}
204216
205217static int nvme_submit_user_cmd (struct request_queue * q ,
206- struct nvme_command * cmd , u64 ubuffer ,
207- unsigned bufflen , void __user * meta_buffer , unsigned meta_len ,
208- u32 meta_seed , u64 * result , unsigned timeout , bool vec )
218+ struct nvme_command * cmd , u64 ubuffer , unsigned bufflen ,
219+ void __user * meta_buffer , unsigned meta_len , u32 meta_seed ,
220+ u64 * result , unsigned timeout , unsigned int flags )
209221{
210222 struct nvme_ctrl * ctrl ;
211223 struct request * req ;
@@ -221,7 +233,7 @@ static int nvme_submit_user_cmd(struct request_queue *q,
221233 req -> timeout = timeout ;
222234 if (ubuffer && bufflen ) {
223235 ret = nvme_map_user_request (req , ubuffer , bufflen , meta_buffer ,
224- meta_len , meta_seed , & meta , NULL , vec );
236+ meta_len , meta_seed , & meta , NULL , flags );
225237 if (ret )
226238 return ret ;
227239 }
@@ -304,10 +316,8 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
304316 c .rw .apptag = cpu_to_le16 (io .apptag );
305317 c .rw .appmask = cpu_to_le16 (io .appmask );
306318
307- return nvme_submit_user_cmd (ns -> queue , & c ,
308- io .addr , length ,
309- metadata , meta_len , lower_32_bits (io .slba ), NULL , 0 ,
310- false);
319+ return nvme_submit_user_cmd (ns -> queue , & c , io .addr , length , metadata ,
320+ meta_len , lower_32_bits (io .slba ), NULL , 0 , 0 );
311321}
312322
313323static bool nvme_validate_passthru_nsid (struct nvme_ctrl * ctrl ,
@@ -325,7 +335,8 @@ static bool nvme_validate_passthru_nsid(struct nvme_ctrl *ctrl,
325335}
326336
327337static int nvme_user_cmd (struct nvme_ctrl * ctrl , struct nvme_ns * ns ,
328- struct nvme_passthru_cmd __user * ucmd , fmode_t mode )
338+ struct nvme_passthru_cmd __user * ucmd , unsigned int flags ,
339+ fmode_t mode )
329340{
330341 struct nvme_passthru_cmd cmd ;
331342 struct nvme_command c ;
@@ -353,16 +364,15 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
353364 c .common .cdw14 = cpu_to_le32 (cmd .cdw14 );
354365 c .common .cdw15 = cpu_to_le32 (cmd .cdw15 );
355366
356- if (!nvme_cmd_allowed (ns , & c , mode ))
367+ if (!nvme_cmd_allowed (ns , & c , 0 , mode ))
357368 return - EACCES ;
358369
359370 if (cmd .timeout_ms )
360371 timeout = msecs_to_jiffies (cmd .timeout_ms );
361372
362373 status = nvme_submit_user_cmd (ns ? ns -> queue : ctrl -> admin_q , & c ,
363- cmd .addr , cmd .data_len ,
364- nvme_to_user_ptr (cmd .metadata ), cmd .metadata_len ,
365- 0 , & result , timeout , false);
374+ cmd .addr , cmd .data_len , nvme_to_user_ptr (cmd .metadata ),
375+ cmd .metadata_len , 0 , & result , timeout , 0 );
366376
367377 if (status >= 0 ) {
368378 if (put_user (result , & ucmd -> result ))
@@ -373,8 +383,8 @@ static int nvme_user_cmd(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
373383}
374384
375385static int nvme_user_cmd64 (struct nvme_ctrl * ctrl , struct nvme_ns * ns ,
376- struct nvme_passthru_cmd64 __user * ucmd , bool vec ,
377- fmode_t mode )
386+ struct nvme_passthru_cmd64 __user * ucmd , unsigned int flags ,
387+ fmode_t mode )
378388{
379389 struct nvme_passthru_cmd64 cmd ;
380390 struct nvme_command c ;
@@ -401,16 +411,15 @@ static int nvme_user_cmd64(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
401411 c .common .cdw14 = cpu_to_le32 (cmd .cdw14 );
402412 c .common .cdw15 = cpu_to_le32 (cmd .cdw15 );
403413
404- if (!nvme_cmd_allowed (ns , & c , mode ))
414+ if (!nvme_cmd_allowed (ns , & c , flags , mode ))
405415 return - EACCES ;
406416
407417 if (cmd .timeout_ms )
408418 timeout = msecs_to_jiffies (cmd .timeout_ms );
409419
410420 status = nvme_submit_user_cmd (ns ? ns -> queue : ctrl -> admin_q , & c ,
411- cmd .addr , cmd .data_len ,
412- nvme_to_user_ptr (cmd .metadata ), cmd .metadata_len ,
413- 0 , & cmd .result , timeout , vec );
421+ cmd .addr , cmd .data_len , nvme_to_user_ptr (cmd .metadata ),
422+ cmd .metadata_len , 0 , & cmd .result , timeout , flags );
414423
415424 if (status >= 0 ) {
416425 if (put_user (cmd .result , & ucmd -> result ))
@@ -571,7 +580,7 @@ static int nvme_uring_cmd_io(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
571580 c .common .cdw14 = cpu_to_le32 (READ_ONCE (cmd -> cdw14 ));
572581 c .common .cdw15 = cpu_to_le32 (READ_ONCE (cmd -> cdw15 ));
573582
574- if (!nvme_cmd_allowed (ns , & c , ioucmd -> file -> f_mode ))
583+ if (!nvme_cmd_allowed (ns , & c , 0 , ioucmd -> file -> f_mode ))
575584 return - EACCES ;
576585
577586 d .metadata = READ_ONCE (cmd -> metadata );
@@ -641,9 +650,9 @@ static int nvme_ctrl_ioctl(struct nvme_ctrl *ctrl, unsigned int cmd,
641650{
642651 switch (cmd ) {
643652 case NVME_IOCTL_ADMIN_CMD :
644- return nvme_user_cmd (ctrl , NULL , argp , mode );
653+ return nvme_user_cmd (ctrl , NULL , argp , 0 , mode );
645654 case NVME_IOCTL_ADMIN64_CMD :
646- return nvme_user_cmd64 (ctrl , NULL , argp , false , mode );
655+ return nvme_user_cmd64 (ctrl , NULL , argp , 0 , mode );
647656 default :
648657 return sed_ioctl (ctrl -> opal_dev , cmd , argp );
649658 }
@@ -668,14 +677,14 @@ struct nvme_user_io32 {
668677#endif /* COMPAT_FOR_U64_ALIGNMENT */
669678
670679static int nvme_ns_ioctl (struct nvme_ns * ns , unsigned int cmd ,
671- void __user * argp , fmode_t mode )
680+ void __user * argp , unsigned int flags , fmode_t mode )
672681{
673682 switch (cmd ) {
674683 case NVME_IOCTL_ID :
675684 force_successful_syscall_return ();
676685 return ns -> head -> ns_id ;
677686 case NVME_IOCTL_IO_CMD :
678- return nvme_user_cmd (ns -> ctrl , ns , argp , mode );
687+ return nvme_user_cmd (ns -> ctrl , ns , argp , flags , mode );
679688 /*
680689 * struct nvme_user_io can have different padding on some 32-bit ABIs.
681690 * Just accept the compat version as all fields that are used are the
@@ -686,37 +695,40 @@ static int nvme_ns_ioctl(struct nvme_ns *ns, unsigned int cmd,
686695#endif
687696 case NVME_IOCTL_SUBMIT_IO :
688697 return nvme_submit_io (ns , argp );
689- case NVME_IOCTL_IO64_CMD :
690- return nvme_user_cmd64 (ns -> ctrl , ns , argp , false, mode );
691698 case NVME_IOCTL_IO64_CMD_VEC :
692- return nvme_user_cmd64 (ns -> ctrl , ns , argp , true, mode );
699+ flags |= NVME_IOCTL_VEC ;
700+ fallthrough ;
701+ case NVME_IOCTL_IO64_CMD :
702+ return nvme_user_cmd64 (ns -> ctrl , ns , argp , flags , mode );
693703 default :
694704 return - ENOTTY ;
695705 }
696706}
697707
698- static int __nvme_ioctl (struct nvme_ns * ns , unsigned int cmd , void __user * arg ,
699- fmode_t mode )
700- {
701- if (is_ctrl_ioctl (cmd ))
702- return nvme_ctrl_ioctl (ns -> ctrl , cmd , arg , mode );
703- return nvme_ns_ioctl (ns , cmd , arg , mode );
704- }
705-
706708int nvme_ioctl (struct block_device * bdev , fmode_t mode ,
707709 unsigned int cmd , unsigned long arg )
708710{
709711 struct nvme_ns * ns = bdev -> bd_disk -> private_data ;
712+ void __user * argp = (void __user * )arg ;
713+ unsigned int flags = 0 ;
710714
711- return __nvme_ioctl (ns , cmd , (void __user * )arg , mode );
715+ if (bdev_is_partition (bdev ))
716+ flags |= NVME_IOCTL_PARTITION ;
717+
718+ if (is_ctrl_ioctl (cmd ))
719+ return nvme_ctrl_ioctl (ns -> ctrl , cmd , argp , mode );
720+ return nvme_ns_ioctl (ns , cmd , argp , flags , mode );
712721}
713722
714723long nvme_ns_chr_ioctl (struct file * file , unsigned int cmd , unsigned long arg )
715724{
716725 struct nvme_ns * ns =
717726 container_of (file_inode (file )-> i_cdev , struct nvme_ns , cdev );
727+ void __user * argp = (void __user * )arg ;
718728
719- return __nvme_ioctl (ns , cmd , (void __user * )arg , file -> f_mode );
729+ if (is_ctrl_ioctl (cmd ))
730+ return nvme_ctrl_ioctl (ns -> ctrl , cmd , argp , file -> f_mode );
731+ return nvme_ns_ioctl (ns , cmd , argp , 0 , file -> f_mode );
720732}
721733
722734static int nvme_uring_cmd_checks (unsigned int issue_flags )
@@ -806,6 +818,10 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
806818 void __user * argp = (void __user * )arg ;
807819 struct nvme_ns * ns ;
808820 int srcu_idx , ret = - EWOULDBLOCK ;
821+ unsigned int flags = 0 ;
822+
823+ if (bdev_is_partition (bdev ))
824+ flags |= NVME_IOCTL_PARTITION ;
809825
810826 srcu_idx = srcu_read_lock (& head -> srcu );
811827 ns = nvme_find_path (head );
@@ -821,7 +837,7 @@ int nvme_ns_head_ioctl(struct block_device *bdev, fmode_t mode,
821837 return nvme_ns_head_ctrl_ioctl (ns , cmd , argp , head , srcu_idx ,
822838 mode );
823839
824- ret = nvme_ns_ioctl (ns , cmd , argp , mode );
840+ ret = nvme_ns_ioctl (ns , cmd , argp , flags , mode );
825841out_unlock :
826842 srcu_read_unlock (& head -> srcu , srcu_idx );
827843 return ret ;
@@ -846,7 +862,7 @@ long nvme_ns_head_chr_ioctl(struct file *file, unsigned int cmd,
846862 return nvme_ns_head_ctrl_ioctl (ns , cmd , argp , head , srcu_idx ,
847863 file -> f_mode );
848864
849- ret = nvme_ns_ioctl (ns , cmd , argp , file -> f_mode );
865+ ret = nvme_ns_ioctl (ns , cmd , argp , 0 , file -> f_mode );
850866out_unlock :
851867 srcu_read_unlock (& head -> srcu , srcu_idx );
852868 return ret ;
@@ -945,7 +961,7 @@ static int nvme_dev_user_cmd(struct nvme_ctrl *ctrl, void __user *argp,
945961 kref_get (& ns -> kref );
946962 up_read (& ctrl -> namespaces_rwsem );
947963
948- ret = nvme_user_cmd (ctrl , ns , argp , mode );
964+ ret = nvme_user_cmd (ctrl , ns , argp , 0 , mode );
949965 nvme_put_ns (ns );
950966 return ret ;
951967
@@ -962,9 +978,9 @@ long nvme_dev_ioctl(struct file *file, unsigned int cmd,
962978
963979 switch (cmd ) {
964980 case NVME_IOCTL_ADMIN_CMD :
965- return nvme_user_cmd (ctrl , NULL , argp , file -> f_mode );
981+ return nvme_user_cmd (ctrl , NULL , argp , 0 , file -> f_mode );
966982 case NVME_IOCTL_ADMIN64_CMD :
967- return nvme_user_cmd64 (ctrl , NULL , argp , false , file -> f_mode );
983+ return nvme_user_cmd64 (ctrl , NULL , argp , 0 , file -> f_mode );
968984 case NVME_IOCTL_IO_CMD :
969985 return nvme_dev_user_cmd (ctrl , argp , file -> f_mode );
970986 case NVME_IOCTL_RESET :
0 commit comments