Getting sid within an LKM (kernel version = 5.14.0)

MySprtlty·2024년 5월 30일
0

Kernel

목록 보기
10/10

🏷️Getting sid within an LKM

📌getting current process' sid

  • Keep in mind the existence of the PID namespace.
    • pid_nr vs pid_vnr
/*
 * get session id in kernel module
 */

#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/socket.h>
#include <linux/moduleparam.h>
#include <linux/namei.h>
#include <asm/ptrace.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("mysprtlty");
MODULE_DESCRIPTION("get session id");
MODULE_VERSION("0.01");

static inline long int get_virtual_sid(void)
{
	return pid_vnr(task_session(current));
}

static inline long int get_global_sid(void)
{
	return pid_nr(task_session(current));
}

static int __init get_session_id_init(void) 
{
	long int vnr, nr = 0;
	printk(KERN_INFO "[+] get_session_id module inserted successfully!\n");

	/* return virtual pid */
	vnr = get_virtual_sid();	
	printk(KERN_INFO "[+] current virtual session id = %d\n", vnr);

	/* return global pid */
	nr = get_global_sid();
	printk(KERN_INFO "[+] current global session id = %d\n", nr);

	return 0;
}

static void __exit get_session_id_exit(void) 
{
	printk(KERN_INFO "[+] get_session_id module has been unloaded\n");
}

module_init(get_session_id_init);
module_exit(get_session_id_exit);
  • PID (Process ID): 각 프로세스에 할당된 고유한 식별 번호
  • PPID (Parent Process ID): 프로세스의 부모 프로세스의 식별 번호
  • PGID (Process Group ID): 프로세스 그룹 ID로, 보통 파이프라인으로 연결된 명령어들이 같은 PGID를 공유
    • ex) ps -ef | grep "snort"
  • TID (Thread ID): 개별 스레드에 부여된 고유한 식별 번호
  • TGID (Thread Group ID): 스레드 그룹 ID로, 여러 스레드가 속한 프로세스의 ID를 나타냄. The TGID (Thread Group ID) of the main thread of a process is indeed the same as the PID (Process ID) of that process.
  • SID (Session ID): 세션의 식별 번호로, 세션 리더 프로세스의 식별 번호를 사용
profile
2Co 4:7

0개의 댓글