diff --git a/fs/fs.c b/fs/fs.c index 8417067..3b5f284 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -229,6 +229,16 @@ files[f->no].in_use = 0; } +static int check_fd(int fd) +{ + if (fd < 0 || fd >= MAX_FILES || !files[fd].in_use) { + errno = -EBADF; + return errno; + } + + return 0; +} + static struct device_d *get_fs_device_by_path(char **path) { struct device_d *dev; @@ -457,6 +467,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -474,6 +487,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -494,6 +510,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -524,6 +543,9 @@ FILE *f = &files[fildes]; off_t pos; + if (check_fd(fildes)) + return -1; + errno = 0; dev = f->dev; @@ -567,6 +589,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -589,6 +614,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -627,6 +655,9 @@ FILE *f = &files[fd]; void *ret = (void *)-1; + if (check_fd(fd)) + return ret; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data; @@ -646,6 +677,9 @@ struct fs_driver_d *fsdrv; FILE *f = &files[fd]; + if (check_fd(fd)) + return errno; + dev = f->dev; fsdrv = (struct fs_driver_d *)dev->driver->type_data;