Skip to contentSkip to conversation

stack overflow

DISCUSSION Breno 4 participants 8 messages Sep 12, 2003 – Sep 13, 2003

lore
Share & tools
lore

Breno

author
#0 lore raw

Hi ... this is my idea to check a stack overflow. What do you think ?

#define STACK_LIMIT (1024*8192)/PAGE_SIZE

int check_stack_overflow(struct task_struct *tsk)
{

    unsigned long stack_size,stack_addr,stack_ptr;
    int i;

         if(tsk->mm != NULL)
         {
                  stack_addr = tsk->mm->start_stack;

                  stack_ptr = tsk->thread.esp;

                  for(i=0; i < stack_ptr; i++)
                  stack_addr++;

                  stack_size = (stack_addr - stack_ptr)/PAGE_SIZE;

                  if(stack_size > ( STACK_LIMIT - 1))
                  {
                               printk(KERN_CRIT"Process %s : pid %d -
                                Can cause stack
overflow\n",tsk->comm,tsk->pid);
                               return 0;
                  }
         }

return 0;
}

att,
Breno

#1 lore raw
Quoted 7 lines · Breno

Hi ... this is my idea to check a stack overflow. What do you think ?

#define STACK_LIMIT (1024*8192)/PAGE_SIZE

int check_stack_overflow(struct task_struct *tsk)
{

Quoted 15 lines
    unsigned long stack_size,stack_addr,stack_ptr;
    int i;

         if(tsk->mm != NULL)
         {
                  stack_addr = tsk->mm->start_stack;

                  stack_ptr = tsk->thread.esp;

                  for(i=0; i < stack_ptr; i++)
                  stack_addr++;

                  stack_size = (stack_addr - stack_ptr)/PAGE_SIZE;

                  if(stack_size > ( STACK_LIMIT - 1))

Well, with the exception of the fact that STACK_LIMIT is 8MB, and kernel stacks are only 8kB (on i386)...

Also, see "do_IRQ()" (i386) for CONFIG_DEBUG_STACKOVERFLOW to see this already.

Cheers, Andreas
--
Andreas Dilger
http://sourceforge.net/projects/ext2resize/
http://www-mddsp.enel.ucalgary.ca/People/adilger/

#2 lore raw
Quoted 3 lines · Andreas Dilger

Well, with the exception of the fact that STACK_LIMIT is 8MB, and kernel stacks are only 8kB (on i386)...
Also, see "do_IRQ()" (i386) for CONFIG_DEBUG_STACKOVERFLOW to see this already.

What he actually wants is in-kernel user stack overflow checking, which is basically impossible since user stacks are demand paged. He's been told this before and failed to absorb it.

There have been attempts to use i386 segmentation for stack limit checks written but they should probably not be confused with this.

-- wli

#3 lore raw
Quoted 3 lines · William Lee Irwin III

What he actually wants is in-kernel user stack overflow checking, which is basically impossible since user stacks are demand paged. He's been told this before and failed to absorb it.

We will fault and error on a user stack exceed. You need to use sigaltstack to catch it for obvious reasons. You can also use mmap and drop in red zones on user space stacks

#4 lore raw
Quoted 3 lines · William Lee Irwin III
Quoted 3 lines

What he actually wants is in-kernel user stack overflow checking, which is basically impossible since user stacks are demand paged. He's been told this before and failed to absorb it.

Quoted 3 lines · Alan Cox

We will fault and error on a user stack exceed. You need to use sigaltstack to catch it for obvious reasons. You can also use mmap and drop in red zones on user space stacks

Stack rlimits are fine and we already do those; the rest sounds like something userspace has to do.

-- wli

author
#5 lore raw

stack overflow - kernel thread

Hi .... this worked

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/unistd.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/highmem.h>
#include <linux/string.h>
#include <asm/uaccess.h>
#include <asm/errno.h>
#include <asm/mman.h>
#include <asm/page.h>
#include <asm/pgalloc.h>
#include <asm/processor.h>
#include <asm-i386/vm86.h>

#define THREAD 1
#define STACK_LIMIT 8192

int check_stack()
{
struct task_struct *p = NULL;
struct vm_area_struct *vma;

unsigned long stack_size, stack_addr, stack_ptr;
    
    daemonize();
    sprintf(current->comm,"CHECK_STACK");
 
    for(;;schedule()) 
    {
         for_each_task(p)
         {
                 if((p->mm != NULL) && (p->pid != 1))
                 {
  
                     stack_addr = p->mm->start_stack;
  
                     stack_ptr = p->thread.esp;
 
                     vma = find_vma(p->mm,stack_addr);
  
                      if(!vma)
                      return 0;
  
                      stack_size = (vma->vm_end - vma->vm_start)/PAGE_SIZE;
  
                      if(stack_size >= (STACK_LIMIT - 1024))
                      {
                          printk(KERN_CRIT"Process %s pid: %d\n",p->comm,p->pid);
                          printk(KERN_CRIT"Stack size %lu k\n",stack_size);
      
                          vma->vm_flags &= ~(VM_WRITE | VM_GROWSDOWN);
      
                          return 0;
                       }
     
                   }
            }
    }         

return 0;
}

int init_module()
{
int th_pid[THREAD];
int i;

    for(i = 0;i < THREAD;i++)
    th_pid[i] = kernel_thread(check_stack,NULL,CLONE_SIGHAND);

if(th_pid[i] < 0)
printk(KERN_CRIT"Cannot create thread\n");

return 0;
}

void cleanup_module()
{
}

att,
Breno
----- Original Message -----
From: "William Lee Irwin III" <[email protected]>
To: "Alan Cox" <[email protected]>
Cc:
"Breno" <[email protected]>; "Linux Kernel Mailing List" <[email protected]>
Sent: Saturday, September 13, 2003 12:25 AM
Subject: Re: stack overflow

Quoted 20 lines · William Lee Irwin III
Quoted 3 lines
Quoted 3 lines

What he actually wants is in-kernel user stack overflow checking, which is basically impossible since user stacks are demand paged. He's been told this before and failed to absorb it.

Quoted 3 lines · Alan Cox

We will fault and error on a user stack exceed. You need to use sigaltstack to catch it for obvious reasons. You can also use mmap and drop in red zones on user space stacks

Stack rlimits are fine and we already do those; the rest sounds like something userspace has to do.

-- wli
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#6 lore raw
In reply to William Lee Irwin III

Wli,

Exactly that stack users are demand paged , you can calculate the size of stack. This is will impossible or more difficult to do if you have more that one mm->start_stack :)

att
Breno

----- Original Message -----
From: "William Lee Irwin III" <[email protected]>
To: "Breno" <[email protected]>; "Kernel List" <[email protected]>
Sent: Saturday, September 13, 2003 12:06 AM
Subject: Re: stack overflow

Quoted 4 lines · Andreas Dilger
Quoted 3 lines

Well, with the exception of the fact that STACK_LIMIT is 8MB, and kernel stacks are only 8kB (on i386)...
Also, see "do_IRQ()" (i386) for CONFIG_DEBUG_STACKOVERFLOW to see this

already.

Quoted 15 lines

What he actually wants is in-kernel user stack overflow checking, which is basically impossible since user stacks are demand paged. He's been told this before and failed to absorb it.

There have been attempts to use i386 segmentation for stack limit checks written but they should probably not be confused with this.

-- wli
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

author
#7 lore raw
In reply to Andreas Dilger

I think that size limit of user stack is 8mb

Breno
----- Original Message -----
From: "Andreas Dilger" <[email protected]>
To: "Breno" <[email protected]>
Cc: "Kernel List" <[email protected]>
Sent: Friday, September 12, 2003 11:50 PM
Subject: Re: stack overflow

Quoted 20 lines · Breno
Quoted 19 lines

Hi ... this is my idea to check a stack overflow. What do you think ?

#define STACK_LIMIT (1024*8192)/PAGE_SIZE

int check_stack_overflow(struct task_struct *tsk)
{

    unsigned long stack_size,stack_addr,stack_ptr;
    int i;

         if(tsk->mm != NULL)
         {
                  stack_addr = tsk->mm->start_stack;

                  stack_ptr = tsk->thread.esp;

                  for(i=0; i < stack_ptr; i++)
                  stack_addr++;
Quoted 8 lines
Quoted 3 lines
                  stack_size = (stack_addr - stack_ptr)/PAGE_SIZE;

                  if(stack_size > ( STACK_LIMIT - 1))

Well, with the exception of the fact that STACK_LIMIT is 8MB, and kernel stacks are only 8kB (on i386)...

Also, see "do_IRQ()" (i386) for CONFIG_DEBUG_STACKOVERFLOW to see this

already.

Quoted 12 lines

Cheers, Andreas
--
Andreas Dilger
http://sourceforge.net/projects/ext2resize/
http://www-mddsp.enel.ucalgary.ca/People/adilger/

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/