tech-net archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Proposal to automatically make the owner/user of an accepted socket the current process



In article <F63D1A2B-D725-4ED8-9C26-C48587809C77%me.com@localhost>,
Jason Thorpe  <thorpej%me.com@localhost> wrote:
>
>
>> On Jun 5, 2025, at 7:51â?¯PM, Emmanuel Nyarko <emmankoko519%gmail.com@localhost> wrote:
>> 
>> And also, fchown, no support for it yet ? 
>
>fchown()?  It changes the uid/gid of a file on disk, like chown() but
>takes a file descriptor rather than a path.

Try this on linux as root. sockets contain so_cred so presumably
adding fchown to fileops will let you do that. FreeBSD already has fchown on
their fileops, but they make it return EINVAL for sockets.

christos

#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <err.h>


int
main(void)
{
	struct stat st;
	int s = socket(PF_INET, SOCK_STREAM, 0);
	if (s == -1)
		err(EXIT_FAILURE, "socket");
	if (fstat(s, &st) == -1)
		err(EXIT_FAILURE, "fstat");

	printf("uid=%d gid=%d\n", st.st_uid, st.st_gid);
	if (fchown(s, 100, 100) == -1)
		err(EXIT_FAILURE, "fchown");
	if (fstat(s, &st) == -1)
		err(EXIT_FAILURE, "fstat");

	printf("uid=%d gid=%d\n", st.st_uid, st.st_gid);
	return 0;
}



Home | Main Index | Thread Index | Old Index