diff --git a/fs/fcntl.c b/fs/fcntl.c
index 22dd9dcce7ecc8536afd9dd59b8aaf4644babb29..3d89de31066ae030ff3d4d3ca2cfe44e1c81539e 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -397,6 +397,9 @@ static long f_dupfd_query(int fd, struct file *filp)
 {
 	CLASS(fd_raw, f)(fd);
 
+	if (fd_empty(f))
+		return -EBADF;
+
 	/*
 	 * We can do the 'fdput()' immediately, as the only thing that
 	 * matters is the pointer value which isn't changed by the fdput.