Skip to contentSkip to conversation

Fix mmap_kmem (was: [question] What's the difference between /dev/km

PATCH Chuck Ebbert 2 participants 2 messages Aug 12, 2005

lore
Share & tools
lore
#0 lore raw
Quoted 6 lines · Hugh Dickins
Quoted 3 lines · Steven Rostedt

Found the problem. It is a bug with mmap_kmem. The order of checks is wrong, so here's the patch.

Good find, looks right to me, so far as it goes (why does this check

Even with Steven's patch applied, root cannot read from /dev/kmem with the
following program, which works on 2.6.9. "Invalid argument" means that the "fd is attached to an object that is unsuitable for reading."

# ./desc2
GDT is at 0xc13097a0
read /dev/kmem: Invalid argument

#include <stdio.h>
#include <fcntl.h>
#define perr(err) { perror(err), exit(1); }

struct {
        unsigned short limit;
        unsigned int base;
} __attribute__ ((packed)) dtr;

int kmem, i;
int buf[1024];

int main() {
        if ((kmem = open("/dev/kmem", O_RDONLY)) < 0)
                perr("open /dev/kmem");

        asm("sgdtl %0" : "=m"(dtr));
        printf("GDT is at 0x%x\n", dtr.base);
        dtr.limit++;  /* convert to size */

        if (lseek(kmem, dtr.base, SEEK_SET) != dtr.base)
                perr("lseek /dev/kmem");
        if (read(kmem, buf, dtr.limit) != dtr.limit)
                perr("read /dev/kmem");

        for (i = 0; i < dtr.limit / 4; i += 2)
                if (buf[i+1])
                        printf("entry %d(0x%x): 0x%x 0x%x\n",
                                i / 2, i / 2, buf[i], buf[i+1]);

        return 0;
}

__
Chuck

#1 lore raw
Quoted 4 lines · Chuck Ebbert

Even with Steven's patch applied, root cannot read from /dev/kmem with the
following program, which works on 2.6.9. "Invalid argument" means that the "fd is attached to an object that is unsuitable for reading."

Yeah, I noticed this too. But my programs were only mmaping kmem, and not reading it, so I didn't bother to fix that. When I get a chance, I could also look into that too. But I guess the next thing to do is send in a patch that makes kmem a config option, with the default off.

-- Steve