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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-13-2005, 01:06 PM
|
#1
|
|
LQ Newbie
Registered: Dec 2005
Posts: 13
Rep:
|
raw packet sending & receiving
I have need to open a raw socket for link layer level packet sending and receiving.
mysock= socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL);
However, man raw(7) tell me:
"Only processes with an effective user id of 0 or the CAP_NET_RAW capability are allowed to open raw sockets."
But eventually I need to run this program in user mode also. And I have to log into the su mode to create this socket. Any idea how to resolve this?
sjcoder
|
|
|
|
12-13-2005, 07:58 PM
|
#2
|
|
Member
Registered: May 2004
Posts: 552
Rep:
|
Possibly two ways, run the program suid so it has the proper permission no matter who starts it. Or, create a daemon running as root that does the actual raw socket work and talk to the daemon over a regular socket from your user application.
|
|
|
|
12-13-2005, 11:27 PM
|
#3
|
|
Member
Registered: Jun 2005
Posts: 542
Rep:
|
Remember to drop privileges after getting the raw socket if you make your program setuid. Just make it the 1st thing your program does, then drop 'em.
|
|
|
|
01-05-2006, 05:19 PM
|
#4
|
|
LQ Newbie
Registered: Dec 2005
Posts: 13
Original Poster
Rep:
|
"run the program suid so it has the proper permission no matter who starts it."
To follow up the discussion here. I have tried to open the raw socket I mentioned above with a project I created from KDevelop environment . I made setuid(0) before the socket() call, setuid(0) call retuned -1. Anyone give me a hint about the right way to do it.
Running a daemon at root would be too complicated for me.
Or should I write a driver module for this raw socket communication?
All your inputs are appreciated.
sjcoder
|
|
|
|
01-05-2006, 05:24 PM
|
#5
|
|
Member
Registered: Jun 2005
Posts: 542
Rep:
|
setuid(0) fails because you don't have the privileges. You need to set the set-uid bit on the executable: "chmod u+s /path/to/exec". Then use "setuid(getuid());" in your code to drop privileges.
|
|
|
|
01-05-2006, 06:25 PM
|
#6
|
|
LQ Newbie
Registered: Dec 2005
Posts: 13
Original Poster
Rep:
|
(1) By "chmod u+s /path/to/exec", meaning I have to do this in the command line? Is anyway to resolve this in the code?
(2) Then use "setuid(getuid());" in your code to drop privileges.
I think I need do the best to drop the previlage before exit. What is the consequnce if some exception happen that made me missing the drop call?
Thanks,
sjcoder
|
|
|
|
01-05-2006, 06:44 PM
|
#7
|
|
Member
Registered: Jun 2005
Posts: 542
Rep:
|
Quote:
|
Originally Posted by sjcoder
(1) By "chmod u+s /path/to/exec", meaning I have to do this in the command line? Is anyway to resolve this in the code?
|
No program can get root privileges by itself. It must have been granted by root manually.
Quote:
(2) Then use "setuid(getuid());" in your code to drop privileges.
I think I need do the best to drop the previlage before exit. What is the consequnce if some exception happen that made me missing the drop call?
|
I guess you only need the privileges to get the raw sockets. Dropping them right after obtaining it conforms to the best security practice. Then you can safely register signal handlers and do stuff without the risk of compromise in your program. Remember to setrlimit() with RLIMIT_CORE so any core file won't contain passwords and sensitive data that is passing through your network interfaces.
Thanks,
sjcoder[/QUOTE]
|
|
|
|
01-11-2006, 04:07 AM
|
#8
|
|
LQ Newbie
Registered: Dec 2005
Posts: 13
Original Poster
Rep:
|
Want continue on this topic. After I open the raw socket as I mentioned. I set the sock to be IFF_PROMISC after socket() call. I then wrote a function to recieve the package. but I found out the recvfrom() call is really slow/stucked from time to time. Is anyone know what is the problem? I did not find the option/flag to set recvfrom() call do not wait even data is not availabe.
int receive_pkt(void *pCurBuff, size_t uiSize)
{
int iNumRecv = 0;
struct sockaddr_ll ll;
socklen_t len = sizeof(struct sockaddr_ll);
memset(&ll, 0, len);
#ifdef DEBUG_OUTPUT
printf("receive_pkt(): sock=%d\n", sock);
#endif
// Receive the User Buffer from the Source Address, Check if Error Returned
iNumRecv = recvfrom(sock, pCurBuff, uiSize, 0, (sockaddr*)&ll, &len);
......
}
sjcoder
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:59 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|