/* * Stas Sergeev * * test sigaltstack(SS_ONSTACK | SS_AUTODISARM) * If that succeeds, then swapcontext() can be used inside sighandler safely. * */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #ifndef SS_AUTODISARM #define SS_AUTODISARM (1U << 31) #endif static void *sstack, *ustack; static ucontext_t uc, sc; static const char *msg = "[OK]\tStack preserved"; static const char *msg2 = "[FAIL]\tStack corrupted"; struct stk_data { char msg[128]; int flag; }; void my_usr1(int sig, siginfo_t *si, void *u) { char *aa; int err; stack_t stk; struct stk_data *p; register unsigned long sp asm("sp"); if (sp < (unsigned long)sstack || sp >= (unsigned long)sstack + SIGSTKSZ) { printf("[FAIL]\tSP is not on sigaltstack\n"); exit(EXIT_FAILURE); } /* put some data on stack. other sighandler will try to overwrite it */ aa = alloca(1024); assert(aa); p = (struct stk_data *)(aa + 512); strcpy(p->msg, msg); p->flag = 1; printf("[RUN]\tsignal USR1\n"); err = sigaltstack(NULL, &stk); if (err) { perror("[FAIL]\tsigaltstack()"); exit(EXIT_FAILURE); } if (stk.ss_flags != SS_DISABLE) printf("[FAIL]\tss_flags=%i, should be SS_DISABLE\n", stk.ss_flags); else printf("[OK]\tsigaltstack is disabled in sighandler\n"); swapcontext(&sc, &uc); printf("%s\n", p->msg); if (!p->flag) { printf("[RUN]\tAborting\n"); exit(EXIT_FAILURE); } } void my_usr2(int sig, siginfo_t *si, void *u) { char *aa; struct stk_data *p; printf("[RUN]\tsignal USR2\n"); aa = alloca(1024); /* dont run valgrind on this */ /* try to find the data stored by previous sighandler */ p = memmem(aa, 1024, msg, strlen(msg)); if (p) { printf("[FAIL]\tsigaltstack re-used\n"); /* corrupt the data */ strcpy(p->msg, msg2); /* tell other sighandler that his data is corrupted */ p->flag = 0; } } static void switch_fn(void) { printf("[RUN]\tswitched to user ctx\n"); raise(SIGUSR2); setcontext(&sc); } int main(void) { struct sigaction act; stack_t stk; int err; sigemptyset(&act.sa_mask); act.sa_flags = SA_ONSTACK | SA_SIGINFO; act.sa_sigaction = my_usr1; sigaction(SIGUSR1, &act, NULL); act.sa_sigaction = my_usr2; sigaction(SIGUSR2, &act, NULL); sstack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); if (sstack == MAP_FAILED) { perror("mmap()"); return EXIT_FAILURE; } err = sigaltstack(NULL, &stk); if (err) { perror("[FAIL]\tsigaltstack()"); exit(EXIT_FAILURE); } if (stk.ss_flags == SS_DISABLE) { printf("[OK]\tInitial sigaltstack state was SS_DISABLE\n"); } else { printf("[FAIL]\tInitial sigaltstack state was %i; should have been SS_DISABLE\n", stk.ss_flags); return EXIT_FAILURE; } stk.ss_sp = sstack; stk.ss_size = SIGSTKSZ; stk.ss_flags = SS_ONSTACK | SS_AUTODISARM; err = sigaltstack(&stk, NULL); if (err) { if (errno == EINVAL) { printf("[NOTE]\tThe running kernel doesn't support SS_AUTODISARM\n"); /* * If test cases for the !SS_AUTODISARM variant were * added, we could still run them. We don't have any * test cases like that yet, so just exit and report * success. */ return 0; } else { perror("[FAIL]\tsigaltstack(SS_ONSTACK | SS_AUTODISARM)"); return EXIT_FAILURE; } } ustack = mmap(NULL, SIGSTKSZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0); if (ustack == MAP_FAILED) { perror("mmap()"); return EXIT_FAILURE; } getcontext(&uc); uc.uc_link = NULL; uc.uc_stack.ss_sp = ustack; uc.uc_stack.ss_size = SIGSTKSZ; makecontext(&uc, switch_fn, 0); raise(SIGUSR1); err = sigaltstack(NULL, &stk); if (err) { perror("[FAIL]\tsigaltstack()"); exit(EXIT_FAILURE); } if (stk.ss_flags != SS_AUTODISARM) { printf("[FAIL]\tss_flags=%i, should be SS_AUTODISARM\n", stk.ss_flags); exit(EXIT_FAILURE); } printf("[OK]\tsigaltstack is still SS_AUTODISARM after signal\n"); printf("[OK]\tTest passed\n"); return 0; }
authorMauro Carvalho Chehab <mchehab@s-opensource.com>2016-12-15 08:38:35 -0200
committerMauro Carvalho Chehab <mchehab@s-opensource.com>2016-12-15 08:38:35 -0200
commit65390ea01ce678379da32b01f39fcfac4903f256 (patch)
tree7f849d66121533c331cf61136b124218d87cbf86 /include/net/psnap.h
parente7aa8c2eb11ba69b1b69099c3c7bd6be3087b0ba (diff)
parentd183e4efcae8d88a2f252e546978658ca6d273cc (diff)
Merge branch 'patchwork' into v4l_for_linus
* patchwork: (496 commits) [media] v4l: tvp5150: Add missing break in set control handler [media] v4l: tvp5150: Don't inline the tvp5150_selmux() function [media] v4l: tvp5150: Compile tvp5150_link_setup out if !CONFIG_MEDIA_CONTROLLER [media] em28xx: don't store usb_device at struct em28xx [media] em28xx: use usb_interface for dev_foo() calls [media] em28xx: don't change the device's name [media] mn88472: fix chip id check on probe [media] mn88473: fix chip id check on probe [media] lirc: fix error paths in lirc_cdev_add() [media] s5p-mfc: Add support for MFC v8 available in Exynos 5433 SoCs [media] s5p-mfc: Rework clock handling [media] s5p-mfc: Don't keep clock prepared all the time [media] s5p-mfc: Kill all IS_ERR_OR_NULL in clocks management code [media] s5p-mfc: Remove dead conditional code [media] s5p-mfc: Ensure that clock is disabled before turning power off [media] s5p-mfc: Remove special clock rate management [media] s5p-mfc: Use printk_ratelimited for reporting ioctl errors [media] s5p-mfc: Set DMA_ATTR_ALLOC_SINGLE_PAGES [media] vivid: Set color_enc on HSV formats [media] v4l2-tpg: Init hv_enc field with a valid value ...
Diffstat (limited to 'include/net/psnap.h')