Skip to content

Commit 6b50b65

Browse files
riptlripatel-fd
authored andcommitted
vinyl: add io_uring restrictions
Restrict io_uring usage to READ against the bstream fd. (Similar to seccomp with regular syscalls)
1 parent da6fa25 commit 6b50b65

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/discof/vinyl/fd_vinyl_tile.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ vinyl_io_uring_params( struct io_uring_params * params,
5656
params->cq_entries = uring_depth;
5757
params->flags |= IORING_SETUP_COOP_TASKRUN;
5858
params->flags |= IORING_SETUP_SINGLE_ISSUER;
59+
params->flags |= IORING_SETUP_R_DISABLED;
5960
params->features |= IORING_SETUP_DEFER_TASKRUN;
6061
return params;
6162
}
@@ -175,6 +176,22 @@ vinyl_io_uring_init( fd_vinyl_tile_ctx_t * ctx,
175176

176177
/* Setup io_uring file access */
177178
FD_TEST( 0==io_uring_register_files( ctx->ring, &dev_fd, 1 ) );
179+
180+
/* Register restrictions */
181+
struct io_uring_restriction res[3] = {
182+
{ .opcode = IORING_RESTRICTION_SQE_OP,
183+
.sqe_op = IORING_OP_READ },
184+
{ .opcode = IORING_RESTRICTION_SQE_FLAGS_REQUIRED,
185+
.sqe_flags = IOSQE_FIXED_FILE },
186+
{ .opcode = IORING_RESTRICTION_SQE_FLAGS_ALLOWED,
187+
.sqe_flags = IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS }
188+
};
189+
int res_err = io_uring_register_restrictions( ctx->ring, res, 3U );
190+
if( FD_UNLIKELY( res_err<0 ) ) FD_LOG_ERR(( "io_uring_register_restrictions failed (%i-%s)", res_err, fd_io_strerror( -res_err ) ));
191+
192+
/* Enable rings */
193+
int enable_err = io_uring_enable_rings( ctx->ring );
194+
if( FD_UNLIKELY( enable_err<0 ) ) FD_LOG_ERR(( "io_uring_enable_rings failed (%i-%s)", enable_err, fd_io_strerror( -enable_err ) ));
178195
}
179196

180197
#else /* no io_uring */

src/vinyl/io/fd_vinyl_io_ur.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,6 @@ ur_prep_read( fd_vinyl_io_ur_t * ur,
185185

186186
/* Map seq0 into the bstream store. */
187187

188-
int dev_fd = ur->dev_fd;
189188
ulong dev_base = ur->dev_base;
190189
ulong dev_sz = ur->dev_sz;
191190

@@ -199,7 +198,8 @@ ur_prep_read( fd_vinyl_io_ur_t * ur,
199198
rd->tsz = (uint)rsz;
200199
struct io_uring_sqe * sqe = io_uring_get_sqe( ring );
201200
if( FD_UNLIKELY( !sqe ) ) FD_LOG_CRIT(( "io_uring_get_sqe() returned NULL despite io_uring_sq_space_left()>=2" ));
202-
io_uring_prep_read( sqe, dev_fd, rd->dst, (uint)rsz, dev_base+dev_off );
201+
io_uring_prep_read( sqe, 0, rd->dst, (uint)rsz, dev_base+dev_off );
202+
io_uring_sqe_set_flags( sqe, IOSQE_FIXED_FILE );
203203
io_uring_sqe_set_data( sqe, rd );
204204
ur->sq_prep_cnt++;
205205
if( FD_LIKELY( !sz ) ) return 1U; /* optimize for the unfragmented case */
@@ -210,15 +210,16 @@ ur_prep_read( fd_vinyl_io_ur_t * ur,
210210
the head read job also succeeded. Also, set the low bit of the
211211
userdata to 1 (usually guaranteed to be 0 due to alignment), to
212212
indicate that this SQE is a head frag. */
213-
io_uring_sqe_set_flags( sqe, IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS );
213+
io_uring_sqe_set_flags( sqe, IOSQE_FIXED_FILE | IOSQE_IO_LINK | IOSQE_CQE_SKIP_SUCCESS );
214214
io_uring_sqe_set_data64( sqe, (ulong)rd+1UL );
215215
ur->cq_cnt++; /* Treat as already-completed in metrics */
216216

217217
/* Prepare the tail SQE */
218218
rd->tsz = (uint)sz;
219219
sqe = io_uring_get_sqe( ring );
220220
if( FD_UNLIKELY( !sqe ) ) FD_LOG_CRIT(( "io_uring_get_sqe() returned NULL despite io_uring_sq_space_left()>=2" ));
221-
io_uring_prep_read( sqe, dev_fd, (uchar *)rd->dst + rsz, (uint)sz, dev_base );
221+
io_uring_prep_read( sqe, 0, (uchar *)rd->dst + rsz, (uint)sz, dev_base );
222+
io_uring_sqe_set_flags( sqe, IOSQE_FIXED_FILE );
222223
io_uring_sqe_set_data( sqe, rd );
223224
ur->sq_prep_cnt++;
224225
return 2U;

src/vinyl/io/test_vinyl_io_ur.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ main( int argc,
8787
FD_LOG_ERR(( "io_uring_queue_init_params failed (%i-%s)", init_err, fd_io_strerror( -init_err ) ));
8888
}
8989

90+
FD_TEST( 0==io_uring_register_files( ring, &fd, 1 ) );
91+
9092
ulong align = fd_vinyl_io_ur_align();
9193
FD_TEST( fd_ulong_is_pow2( align ) );
9294
ulong footprint = fd_vinyl_io_ur_footprint( spad_max );

0 commit comments

Comments
 (0)