LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2013, 10:05 AM   #1
webquinty
Member
 
Registered: Apr 2008
Location: Espaņa
Distribution: Suse
Posts: 227

Rep: Reputation: 32
is it possible to detect an illegal address?


Hello,

I would like to check if address is legal or not.
For example, one variable of my program is 0x08453212 is legal but 0x06000000 is an illegal address.

I have tried to detect a illegal address with this code:

Code:
addr_writeable = read(fd_zero, (UDINT *)(rcv_data + 0), 1 );
but always return me 1.

is there any way to detect a illgeal address access??

Best regards.
 
Old 01-23-2013, 10:43 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
'read' has nothing to do with memory addresses. When you access an invalid address you get a SIGSEGV signal.
 
Old 01-24-2013, 04:17 AM   #3
webquinty
Member
 
Registered: Apr 2008
Location: Espaņa
Distribution: Suse
Posts: 227

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by NevemTeve View Post
'read' has nothing to do with memory addresses. When you access an invalid address you get a SIGSEGV signal.
As I understood, system emit signal when SIGSEGV is emit, but after catch the signal, program continue in the same line and execute illegal command again and program crash.

is it correct?
 
Old 01-24-2013, 04:35 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Normally, SIGSEGV stops the program, but you might to try to handle it with a signal-handler (see sigaction(2) for details).

additional reading: http://en.wikipedia.org/wiki/Segmentation_fault
 
Old 01-24-2013, 07:04 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
This is precisely what you should do: just go ahead and attempt the operation. If it fails for this reason, a SIGSEGV will be thrown, and you can catch it.

You don't need to try to "test to see if the address will be valid." Just go for it. Nearly all the time, the operation will be successful and you can just go on your merry way. The exception to that rule is when the address isn't valid ... and that's one reason why we call 'em "exceptions."
 
Old 01-28-2013, 09:03 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you can't catch a SIGSEGV (edit: rubbish)

or maybe you can...

there is example code on the man page on how to do it:

http://linux.die.net/man/2/mprotect

Last edited by bigearsbilly; 01-28-2013 at 09:23 AM. Reason: talking rubbish
 
Old 01-28-2013, 11:02 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
actually, I got interested, read detects bad memory addresses anyway...

Code:
int
main(int argc, char *argv[])
{
    int n = 0;
    char buffer[1024];
    char *p = "hello fishes";
    // p = buffer;

    // set_handler();

    int fd = open(argv[1], 0, 0);
    printf("read %d\n", fd);

    while ( (n = read(fd, p, 2)) ) {

        if (n == -1) {
            switch (errno) {
                case EFAULT:
                    fprintf(stderr, "BAD MEMORY DETECTED\n");

                default:
                    perror("read:");
            }
            break;
        } else {
            printf("%s", p);
        }
    }

    //*p = 'x'; // oops here

    printf("bye\n");

    exit(EXIT_SUCCESS);
}
Code:
[billy@elmer:0]$ ./sigsev /etc/hosts 
read 3
BAD MEMORY DETECTED
read:: Bad address
bye
[billy@elmer:0]$ ./sigsev /etc/fish 
read -1
read:: Bad file descriptor
bye
 
1 members found this post helpful.
Old 01-28-2013, 12:22 PM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
read() and other system calls check for validity of the address and return -EFAULT if the address is invalid (ie. inaccessible for given purpose). SIGSEGV is thrown when program tries to access given memory directly as in:
Code:
*(volatile char *)pointer;
webquinty, why do you need to check validity of an address? It sounds to me like you're doing something you should not be doing.
 
Old 01-28-2013, 06:20 PM   #9
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Within kernel mode, yes, address validity checks do occur. The Kernel is special.
 
Old 01-29-2013, 04:49 AM   #10
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
IMHO, checking the "validity" of an address in this way (i.e checking that it's in your virtual address space) is totally useless.

Even if you can verify it's in your address space, this isn't really "valid", since it could be pointing anywhere in your address space, including data you don't intend or want to be overwritten.

Take the time you're using trying to pre-empt these mistakes and spend it making sure your code's correct.
 
Old 01-29-2013, 06:44 AM   #11
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by sundialsvcs View Post
Within kernel mode, yes, address validity checks do occur. The Kernel is special.
Point is, calls to read() won't cause a SIGSEGV, but instead will set errno to EFAULT.
 
Old 01-29-2013, 08:10 AM   #12
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
One "trick" that I have used a lot is to write a few routines of my own to do memory-allocation within an application.

The first routine allocates a guaranteed zero-filled buffer of the specified size plus 8 bytes more. It puts a "4-byte eyecatcher" at the beginning and at the end. The trailing "eyecatcher" begins with a zero-byte. It returns a pointer past the eyecatcher.

Another routine simply checks for the presence of the eyecatcher (at 4 bytes before the passed cannot-be-NULL pointer, and at the end), and throws an exception if not. Otherwise it returns the value passed.

A final routine checks and then disposes-of the buffer passed, doing nothing if NULL.

Each routine also requires a pointer to a character-string constant which is used in the exception-message if thrown.

So what happens? An amazing number of "nassss-s-s-s-ssty little bugssssss-s-s-s-s-ssss" get stomped during development, and/or almost-instantaneously discovered during initial beta-testing. At basically zero cost in performance.
 
Old 01-29-2013, 08:18 AM   #13
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by sundialsvcs View Post
Another routine simply checks for the presence of the eyecatcher (at 4 bytes before the passed cannot-be-NULL pointer, and at the end), and throws an exception if not. Otherwise it returns the value passed.
This of course won't work for arraies if you pass a pointer to an element in the middle of the array. Strings are among this category.
 
Old 01-29-2013, 08:44 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Note: A similar useful tool is Electric Fence.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to detect ip address from last reboot done? pinga123 Linux - Newbie 1 02-22-2011 02:16 AM
whats an illegal address kapsikum Programming 9 05-29-2006 01:04 AM
detect ip address of packets dales79 Linux - Networking 5 01-17-2006 06:29 AM
How to detect IP address via NAT server jerrytw Linux - Networking 7 11-08-2004 04:42 AM
illegal use of my domain address Lui Linux - Security 9 06-17-2004 05:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration