# Doubt about ucred structure in Kernel



## GroupInode (Feb 17, 2012)

I would like to know more about the ucred structure used in FreeBSD kernel for managing user credentials in kernel. I want to know how can use to get the user ID and also from the structure gets its data. 

Thanking in anticipation.


----------



## GroupInode (Feb 20, 2012)

As far as I know, ucred  family of functions is used to manage user credential structures (struct ucred) within kernel.
I saw /usr/src/sys/sys/ucred.h file

```
struct ucred {
	u_int	cr_ref;			/* reference count */
#define	cr_startcopy cr_uid
	uid_t	cr_uid;			/* effective user id */
	uid_t	cr_ruid;		/* real user id */
	uid_t	cr_svuid;		/* saved user id */
	int	cr_ngroups;		/* number of groups */
	gid_t	cr_rgid;		/* real group id */
	gid_t	cr_svgid;		/* saved group id */
	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
	struct prison	*cr_prison;	/* jail(2) */
	void		*cr_pspare;	/* general use */
	u_int		cr_flags;	/* credential flags */
	void 		*cr_pspare2[2];	/* general use 2 */
#define	cr_endcopy	cr_label
	struct label	*cr_label;	/* MAC label */
	struct auditinfo_addr	cr_audit;	/* Audit properties. */
	gid_t	*cr_groups;		/* groups */
	int	cr_agroups;		/* Available groups */
};
```
From the above structure I want to access real user id information.

Basically *I'm* changing the ufs.ko module trying to get information of the user who last modified the file. The change would be made in ufs_itimes_locked() which updates the modified time in the inode structure and when the modified time is changed *I* wanted to store information of who/which user (uid) modifies the file.

I*'*m stuck here as to which function to use get cr_uid/cr_ruid information in kernel space. I also tried getuid() but it is working only in user space not kernel space.

Any help would be appreciated.


----------



## jake_t (Mar 26, 2012)

Hey GroupInode,

I personally haven't worked with ufs_itimes_locked() but could you not just access cr_uid/cr_ruid via the current thread?

IE:

```
uprintf("UID: %d\n", td->td_proc->p_ucred->cr_uid);
```

Hope this helps.


----------



## GroupInode (Mar 27, 2012)

*Solved*

Thank you jake_t for replying to my thread. I had already figured out how to do this: the same way *yo*u are telling. *T*hat is by the cr_ruid field of curthread defined in </usr/src/sys/sys/pcpu.h>. The only thing was, I knew there was a current pointer in Linux, but we didn't know what was the name of the structure.

Thanks anyway


----------

