Skip to content

Commit 9a151d2

Browse files
committed
security: fixing early tty development bugs
1 parent edb70ed commit 9a151d2

File tree

11 files changed

+329
-18
lines changed

11 files changed

+329
-18
lines changed

Config.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
// https://developer.apple.com/documentation/xcode/adding-a-build-configuration-file-to-your-project
1010

1111
VERSION = 0.9.0
12-
BUILD_NUMBER = 20260224.76.US.seanistethered
12+
BUILD_NUMBER = 20260224.98.US.seanistethered

Nyxian.xcodeproj/project.pbxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@
243243
LindChain/ProcEnvironment/Surface/sys/proc/kill.m,
244244
LindChain/ProcEnvironment/Surface/sys/proc/wait4.m,
245245
LindChain/ProcEnvironment/Surface/sys/syscall.m,
246+
LindChain/ProcEnvironment/Surface/tty/attach.m,
246247
LindChain/ProcEnvironment/Surface/tty/lookup.m,
247248
LindChain/ProcEnvironment/Surface/tty/tty.m,
248249
LindChain/ProcEnvironment/syscall.m,

Nyxian/LindChain/LiveContainer/Tweaks/proc_info.h

Lines changed: 188 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#define PROX_FDTYPE_VNODE 1
66
#define PROC_PIDFDVNODEPATHINFO 2
7+
#define PROC_PIDFDSOCKETINFO 3
78
#define PROC_PIDFDPIPEINFO 6
89

910
#define PROC_PIDLISTFDS 1
@@ -77,4 +78,190 @@ struct pipe_fdinfo {
7778
struct pipe_info pipeinfo;
7879
};
7980

80-
#endif
81+
/* https://github.com/apple/darwin-xnu/blob/2ff845c2e033bd0ff64b5b6aa6063a1f8f65aa32/bsd/sys/proc_info.h */
82+
83+
#include <sys/ioctl.h>
84+
#include <sys/un.h>
85+
86+
#define MAX_KCTL_NAME 96
87+
88+
typedef struct in6_addr {
89+
union {
90+
__uint8_t __u6_addr8[16];
91+
__uint16_t __u6_addr16[8];
92+
__uint32_t __u6_addr32[4];
93+
} __u6_addr; /* 128-bit IP6 address */
94+
} in6_addr_t;
95+
96+
97+
struct in_addr {
98+
in_addr_t s_addr;
99+
};
100+
101+
#define INI_IPV4 0x1
102+
#define INI_IPV6 0x2
103+
104+
struct in4in6_addr {
105+
u_int32_t i46a_pad32[3];
106+
struct in_addr i46a_addr4;
107+
};
108+
109+
struct in_sockinfo {
110+
int insi_fport; /* foreign port */
111+
int insi_lport; /* local port */
112+
uint64_t insi_gencnt; /* generation count of this instance */
113+
uint32_t insi_flags; /* generic IP/datagram flags */
114+
uint32_t insi_flow;
115+
116+
uint8_t insi_vflag; /* ini_IPV4 or ini_IPV6 */
117+
uint8_t insi_ip_ttl; /* time to live proto */
118+
uint32_t rfu_1; /* reserved */
119+
/* protocol dependent part */
120+
union {
121+
struct in4in6_addr ina_46;
122+
struct in6_addr ina_6;
123+
} insi_faddr; /* foreign host table entry */
124+
union {
125+
struct in4in6_addr ina_46;
126+
struct in6_addr ina_6;
127+
} insi_laddr; /* local host table entry */
128+
struct {
129+
u_char in4_tos; /* type of service */
130+
} insi_v4;
131+
struct {
132+
uint8_t in6_hlim;
133+
int in6_cksum;
134+
u_short in6_ifindex;
135+
short in6_hops;
136+
} insi_v6;
137+
};
138+
139+
#define TSI_T_REXMT 0 /* retransmit */
140+
#define TSI_T_PERSIST 1 /* retransmit persistence */
141+
#define TSI_T_KEEP 2 /* keep alive */
142+
#define TSI_T_2MSL 3 /* 2*msl quiet time timer */
143+
#define TSI_T_NTIMERS 4
144+
145+
#define TSI_S_CLOSED 0 /* closed */
146+
#define TSI_S_LISTEN 1 /* listening for connection */
147+
#define TSI_S_SYN_SENT 2 /* active, have sent syn */
148+
#define TSI_S_SYN_RECEIVED 3 /* have send and received syn */
149+
#define TSI_S_ESTABLISHED 4 /* established */
150+
#define TSI_S__CLOSE_WAIT 5 /* rcvd fin, waiting for close */
151+
#define TSI_S_FIN_WAIT_1 6 /* have closed, sent fin */
152+
#define TSI_S_CLOSING 7 /* closed xchd FIN; await FIN ACK */
153+
#define TSI_S_LAST_ACK 8 /* had fin and close; await FIN ACK */
154+
#define TSI_S_FIN_WAIT_2 9 /* have closed, fin is acked */
155+
#define TSI_S_TIME_WAIT 10 /* in 2*msl quiet wait after close */
156+
#define TSI_S_RESERVED 11 /* pseudo state: reserved */
157+
158+
struct tcp_sockinfo {
159+
struct in_sockinfo tcpsi_ini;
160+
int tcpsi_state;
161+
int tcpsi_timer[TSI_T_NTIMERS];
162+
int tcpsi_mss;
163+
uint32_t tcpsi_flags;
164+
uint32_t rfu_1; /* reserved */
165+
uint64_t tcpsi_tp; /* opaque handle of TCP protocol control block */
166+
};
167+
168+
struct un_sockinfo {
169+
uint64_t unsi_conn_so; /* opaque handle of connected socket */
170+
uint64_t unsi_conn_pcb; /* opaque handle of connected protocol control block */
171+
union {
172+
struct sockaddr_un ua_sun;
173+
char ua_dummy[SOCK_MAXADDRLEN];
174+
} unsi_addr; /* bound address */
175+
union {
176+
struct sockaddr_un ua_sun;
177+
char ua_dummy[SOCK_MAXADDRLEN];
178+
} unsi_caddr; /* address of socket connected to */
179+
};
180+
181+
struct ndrv_info {
182+
uint32_t ndrvsi_if_family;
183+
uint32_t ndrvsi_if_unit;
184+
char ndrvsi_if_name[IF_NAMESIZE];
185+
};
186+
187+
struct kern_event_info {
188+
uint32_t kesi_vendor_code_filter;
189+
uint32_t kesi_class_filter;
190+
uint32_t kesi_subclass_filter;
191+
};
192+
193+
struct kern_ctl_info {
194+
uint32_t kcsi_id;
195+
uint32_t kcsi_reg_unit;
196+
uint32_t kcsi_flags; /* support flags */
197+
uint32_t kcsi_recvbufsize; /* request more than the default buffer size */
198+
uint32_t kcsi_sendbufsize; /* request more than the default buffer size */
199+
uint32_t kcsi_unit;
200+
char kcsi_name[MAX_KCTL_NAME]; /* unique nke identifier, provided by DTS */
201+
};
202+
203+
struct vsock_sockinfo {
204+
uint32_t local_cid;
205+
uint32_t local_port;
206+
uint32_t remote_cid;
207+
uint32_t remote_port;
208+
};
209+
210+
struct sockbuf_info {
211+
uint32_t sbi_cc;
212+
uint32_t sbi_hiwat; /* SO_RCVBUF, SO_SNDBUF */
213+
uint32_t sbi_mbcnt;
214+
uint32_t sbi_mbmax;
215+
uint32_t sbi_lowat;
216+
short sbi_flags;
217+
short sbi_timeo;
218+
};
219+
220+
enum {
221+
SOCKINFO_GENERIC = 0,
222+
SOCKINFO_IN = 1,
223+
SOCKINFO_TCP = 2,
224+
SOCKINFO_UN = 3,
225+
SOCKINFO_NDRV = 4,
226+
SOCKINFO_KERN_EVENT = 5,
227+
SOCKINFO_KERN_CTL = 6,
228+
SOCKINFO_VSOCK = 7,
229+
};
230+
231+
struct socket_info {
232+
struct vinfo_stat soi_stat;
233+
uint64_t soi_so; /* opaque handle of socket */
234+
uint64_t soi_pcb; /* opaque handle of protocol control block */
235+
int soi_type;
236+
int soi_protocol;
237+
int soi_family;
238+
short soi_options;
239+
short soi_linger;
240+
short soi_state;
241+
short soi_qlen;
242+
short soi_incqlen;
243+
short soi_qlimit;
244+
short soi_timeo;
245+
u_short soi_error;
246+
uint32_t soi_oobmark;
247+
struct sockbuf_info soi_rcv;
248+
struct sockbuf_info soi_snd;
249+
int soi_kind;
250+
uint32_t rfu_1; /* reserved */
251+
union {
252+
struct in_sockinfo pri_in; /* SOCKINFO_IN */
253+
struct tcp_sockinfo pri_tcp; /* SOCKINFO_TCP */
254+
struct un_sockinfo pri_un; /* SOCKINFO_UN */
255+
struct ndrv_info pri_ndrv; /* SOCKINFO_NDRV */
256+
struct kern_event_info pri_kern_event; /* SOCKINFO_KERN_EVENT */
257+
struct kern_ctl_info pri_kern_ctl; /* SOCKINFO_KERN_CTL */
258+
struct vsock_sockinfo pri_vsock; /* SOCKINFO_VSOCK */
259+
} soi_proto;
260+
};
261+
262+
struct socket_fdinfo {
263+
struct proc_fileinfo pfi;
264+
struct socket_info psi;
265+
};
266+
267+
#endif // !_PROCINFO_H_

Nyxian/LindChain/ProcEnvironment/Surface/proc/list.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void copy_proc_to_user(ksurface_proc_t *proc,
9393
memcpy(kp, &(proc->kproc.kcproc.bsd), sizeof(kinfo_proc_t));
9494
}
9595

96-
void proc_list_radix_walker_callback(pid_t pid,
96+
void proc_list_radix_walker_callback(uint64_t ident,
9797
void *value,
9898
void *ctx)
9999
{

Nyxian/LindChain/ProcEnvironment/Surface/surface.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ static inline void ksurface_kinit_kinfo(void)
8585
}
8686

8787
/* setting up process radix tree */
88-
klog_log(@"ksurface:kinit:kinfo", @"initilizing radix tree");
88+
klog_log(@"ksurface:kinit:kinfo", @"initilizing radix trees");
8989
ksurface->proc_info.tree.root = NULL;
9090
ksurface->proc_info.proc_count = 0;
91+
ksurface->tty_info.tty.root = NULL;
9192

9293
/* loading hostname from standard user defaults */
9394
NSString *hostname = [[NSUserDefaults standardUserDefaults] stringForKey:@"LDEHostname"];
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Copyright (C) 2025 cr4zyengineer
3+
4+
This file is part of Nyxian.
5+
6+
Nyxian is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Nyxian is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Nyxian. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef TTY_ATTACH_H
21+
#define TTY_ATTACH_H
22+
23+
#import <LindChain/ProcEnvironment/Surface/tty/def.h>
24+
#import <LindChain/ProcEnvironment/Surface/proc/def.h>
25+
26+
ksurface_return_t tty_attach_proc(ksurface_proc_t *proc, ksurface_tty_t *tty);
27+
28+
#endif /* TTY_ATTACH_H */
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
Copyright (C) 2025 cr4zyengineer
3+
4+
This file is part of Nyxian.
5+
6+
Nyxian is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
Nyxian is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with Nyxian. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#import <LindChain/ProcEnvironment/Surface/tty/attach.h>
21+
22+
/* typedef bool (*kvobject_event_handler_t)(kvobject_strong_t*,kvevent_type_t,uint8_t,void*); */
23+
bool tty_proc_event_handler(kvobject_strong_t *kvo,
24+
kvevent_type_t type,
25+
uint8_t value,
26+
void *pld)
27+
{
28+
switch(type)
29+
{
30+
case kvObjEventDeinit:
31+
return true;
32+
default:
33+
return false;
34+
}
35+
}
36+
37+
ksurface_return_t tty_attach_proc(ksurface_proc_t *proc,
38+
ksurface_tty_t *tty)
39+
{
40+
/* retain process */
41+
if(!kvo_retain(proc))
42+
{
43+
return SURFACE_RETAIN_FAILED;
44+
}
45+
46+
/*
47+
* attach to process lifecycle
48+
* and consume callers reference.
49+
*/
50+
kvobject_event_register((kvobject_t*)proc, tty_proc_event_handler, NULL, tty);
51+
52+
53+
kvo_release(proc);
54+
return SURFACE_SUCCESS;
55+
}

Nyxian/LindChain/ProcEnvironment/Surface/tty/def.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct ksurface_tty {
3434
/* file descriptors */
3535
int masterfd;
3636
int slavefd;
37-
uint64_t slavehandle;
37+
uint32_t kslavecid;
3838
};
3939

4040
#endif /* TTY_DEF_H */

Nyxian/LindChain/ProcEnvironment/Surface/tty/lookup.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#define TTY_LOOKUP_H
2222

2323
#import <LindChain/ProcEnvironment/Surface/tty/def.h>
24+
#import <LindChain/Private/mach/fileport.h>
2425

25-
ksurface_return_t tty_for_handle(uint64_t handle, ksurface_tty_t **tty);
26+
ksurface_return_t tty_for_port(fileport_t port, ksurface_tty_t **tty);
2627

2728
#endif /* TTY_LOOKUP_H */

Nyxian/LindChain/ProcEnvironment/Surface/tty/lookup.m

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,41 @@
1919

2020
#import <LindChain/ProcEnvironment/Surface/surface.h>
2121
#import <LindChain/ProcEnvironment/Surface/tty/lookup.h>
22+
#import <LindChain/LiveContainer/Tweaks/libproc.h>
2223

23-
ksurface_return_t tty_for_handle(uint64_t handle,
24-
ksurface_tty_t **tty)
24+
ksurface_return_t tty_for_port(fileport_t port,
25+
ksurface_tty_t **tty)
2526
{
2627
/* sanity check */
2728
if(tty == NULL)
2829
{
2930
return SURFACE_NULLPTR;
3031
}
3132

32-
/* tty lookup */
33+
/* getting file descriptor */
34+
int fd = fileport_makefd(port);
35+
36+
/* validating file descriptor */
37+
if(fd < 0)
38+
{
39+
return SURFACE_FAILED;
40+
}
41+
42+
/* getting unique object pointer */
43+
struct socket_fdinfo si;
44+
45+
if(proc_pidfdinfo(getpid(), fd, PROC_PIDFDSOCKETINFO, &si, sizeof(si)) <= 0)
46+
{
47+
close(fd);
48+
return SURFACE_FAILED;
49+
}
50+
51+
/* disposing that fd, not needed rn */
52+
close(fd);
53+
54+
/* tty tree lookup */
3355
tty_table_rdlock();
34-
*tty = radix_lookup(&(ksurface->tty_info.tty), handle);
56+
*tty = radix_lookup(&(ksurface->tty_info.tty), si.psi.soi_proto.pri_kern_ctl.kcsi_id);
3557
tty_table_unlock();
3658

3759
/*

0 commit comments

Comments
 (0)