mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 00:23:30 +00:00
Organize companion code and materials by book part: part-01/ Foundations part-02/ Kernel & driver fundamentals part-03/ Character & block devices part-04/ Networking & storage part-05/ Buses & advanced topics part-06/ Debugging & performance part-07/ Real-world drivers appendices/ Supplementary material README.md Overview and build instructions
185 lines
4.2 KiB
C
185 lines
4.2 KiB
C
/*
|
|
* secdev.c - Lab 2 of Chapter 31 (starter file).
|
|
*
|
|
* This file is a copy of Lab 1 with "TODO(lab2)" markers added at the
|
|
* two places where you need to add bounds checks. Your task is to make
|
|
* sc_buf-sized reads and writes safe regardless of what the caller asks
|
|
* for.
|
|
*
|
|
* The buffer overflow and the unchecked read length are the only two
|
|
* issues you are asked to fix in this lab. Do not worry about the
|
|
* information leak, the privilege check, the logging rate, or the
|
|
* detach order here; they are covered in later labs.
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/kernel.h>
|
|
#include <sys/module.h>
|
|
#include <sys/systm.h>
|
|
#include <sys/conf.h>
|
|
#include <sys/uio.h>
|
|
#include <sys/malloc.h>
|
|
#include <sys/lock.h>
|
|
#include <sys/mutex.h>
|
|
#include <sys/ioccom.h>
|
|
|
|
#define SECDEV_BUFSIZE 4096
|
|
|
|
struct secdev_info {
|
|
uint32_t version;
|
|
uint64_t flags;
|
|
uint16_t id;
|
|
char name[32];
|
|
};
|
|
|
|
#define SECDEV_GET_INFO _IOR('S', 1, struct secdev_info)
|
|
|
|
struct secdev_softc {
|
|
struct mtx sc_mtx;
|
|
struct cdev *sc_cdev;
|
|
char *sc_buf;
|
|
size_t sc_buflen;
|
|
};
|
|
|
|
static struct secdev_softc secdev_sc;
|
|
|
|
static d_open_t secdev_open;
|
|
static d_close_t secdev_close;
|
|
static d_read_t secdev_read;
|
|
static d_write_t secdev_write;
|
|
static d_ioctl_t secdev_ioctl;
|
|
|
|
static struct cdevsw secdev_cdevsw = {
|
|
.d_version = D_VERSION,
|
|
.d_open = secdev_open,
|
|
.d_close = secdev_close,
|
|
.d_read = secdev_read,
|
|
.d_write = secdev_write,
|
|
.d_ioctl = secdev_ioctl,
|
|
.d_name = "secdev",
|
|
};
|
|
|
|
static int
|
|
secdev_open(struct cdev *dev __unused, int oflags __unused,
|
|
int devtype __unused, struct thread *td __unused)
|
|
{
|
|
|
|
return (0);
|
|
}
|
|
|
|
static int
|
|
secdev_close(struct cdev *dev __unused, int fflag __unused,
|
|
int devtype __unused, struct thread *td __unused)
|
|
{
|
|
|
|
return (0);
|
|
}
|
|
|
|
static int
|
|
secdev_read(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
|
|
{
|
|
struct secdev_softc *sc = &secdev_sc;
|
|
int error;
|
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
/*
|
|
* TODO(lab2): replace the unbounded read with a bounded one. The
|
|
* caller may request more or fewer bytes than sc_buflen; copy out
|
|
* only min(sc_buflen - uio_offset, uio_resid) bytes. If
|
|
* uio_offset is already past the valid data, return 0 bytes.
|
|
*/
|
|
error = uiomove(sc->sc_buf, sc->sc_buflen, uio);
|
|
mtx_unlock(&sc->sc_mtx);
|
|
return (error);
|
|
}
|
|
|
|
static int
|
|
secdev_write(struct cdev *dev __unused, struct uio *uio, int ioflag __unused)
|
|
{
|
|
struct secdev_softc *sc = &secdev_sc;
|
|
int error;
|
|
|
|
mtx_lock(&sc->sc_mtx);
|
|
/*
|
|
* TODO(lab2): clamp the copy length to SECDEV_BUFSIZE. A common
|
|
* pattern is:
|
|
*
|
|
* size_t amount = MIN(uio->uio_resid, SECDEV_BUFSIZE);
|
|
* error = uiomove(sc->sc_buf, amount, uio);
|
|
*
|
|
* Remember to update sc_buflen to reflect how many bytes were
|
|
* actually copied.
|
|
*/
|
|
error = uiomove(sc->sc_buf, uio->uio_resid, uio);
|
|
sc->sc_buflen = SECDEV_BUFSIZE - uio->uio_resid;
|
|
mtx_unlock(&sc->sc_mtx);
|
|
return (error);
|
|
}
|
|
|
|
static int
|
|
secdev_ioctl(struct cdev *dev __unused, u_long cmd, caddr_t data,
|
|
int fflag __unused, struct thread *td __unused)
|
|
{
|
|
struct secdev_info *info;
|
|
|
|
switch (cmd) {
|
|
case SECDEV_GET_INFO:
|
|
info = (struct secdev_info *)data;
|
|
info->version = 1;
|
|
info->flags = 0;
|
|
info->id = 42;
|
|
strncpy(info->name, "secdev", sizeof(info->name));
|
|
return (0);
|
|
default:
|
|
return (ENOTTY);
|
|
}
|
|
}
|
|
|
|
static int
|
|
secdev_modevent(module_t mod __unused, int event, void *arg __unused)
|
|
{
|
|
struct secdev_softc *sc = &secdev_sc;
|
|
struct make_dev_args args;
|
|
int error;
|
|
|
|
switch (event) {
|
|
case MOD_LOAD:
|
|
mtx_init(&sc->sc_mtx, "secdev", NULL, MTX_DEF);
|
|
sc->sc_buf = malloc(SECDEV_BUFSIZE, M_TEMP, M_WAITOK | M_ZERO);
|
|
sc->sc_buflen = 0;
|
|
|
|
make_dev_args_init(&args);
|
|
args.mda_devsw = &secdev_cdevsw;
|
|
args.mda_uid = UID_ROOT;
|
|
args.mda_gid = GID_WHEEL;
|
|
args.mda_mode = 0666;
|
|
|
|
error = make_dev_s(&args, &sc->sc_cdev, "secdev");
|
|
if (error != 0) {
|
|
free(sc->sc_buf, M_TEMP);
|
|
mtx_destroy(&sc->sc_mtx);
|
|
return (error);
|
|
}
|
|
printf("secdev: loaded (Lab 2 starter)\n");
|
|
return (0);
|
|
|
|
case MOD_UNLOAD:
|
|
destroy_dev(sc->sc_cdev);
|
|
free(sc->sc_buf, M_TEMP);
|
|
mtx_destroy(&sc->sc_mtx);
|
|
printf("secdev: unloaded\n");
|
|
return (0);
|
|
|
|
default:
|
|
return (EOPNOTSUPP);
|
|
}
|
|
}
|
|
|
|
static moduledata_t secdev_mod = {
|
|
"secdev",
|
|
secdev_modevent,
|
|
NULL
|
|
};
|
|
|
|
DECLARE_MODULE(secdev, secdev_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|
|
MODULE_VERSION(secdev, 1);
|