net/core/scm.c
Source file repositories/reference/linux-study-clean/net/core/scm.c
File Facts
- System
- Linux kernel
- Corpus path
net/core/scm.c- Extension
.c- Size
- 12613 bytes
- Lines
- 553
- Domain
- Networking Core
- Bucket
- Sockets, Protocols, Packet Path, And Network Policy
- Inferred role
- Networking Core: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Networking stack implementation surface: socket APIs, protocol dispatch, packet flow, routing, filtering, and network namespaces.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/module.hlinux/signal.hlinux/capability.hlinux/errno.hlinux/sched.hlinux/sched/user.hlinux/mm.hlinux/kernel.hlinux/stat.hlinux/socket.hlinux/file.hlinux/fcntl.hlinux/net.hlinux/interrupt.hlinux/netdevice.hlinux/security.hlinux/pid_namespace.hlinux/pid.huapi/linux/pidfd.hlinux/pidfs.hlinux/nsproxy.hlinux/slab.hlinux/errqueue.hlinux/io_uring.hlinux/uaccess.hnet/protocol.hlinux/skbuff.hnet/sock.hnet/compat.hnet/scm.hnet/cls_cgroup.hnet/af_unix.h
Detected Declarations
function setufunction scm_fp_copyfunction __scm_destroyfunction scm_replace_pidfunction __scm_sendfunction for_each_cmsghdrfunction put_cmsgfunction scoped_user_write_access_sizefunction put_cmsg_notruncfunction put_cmsg_scm_timestamping64function put_cmsg_scm_timestampingfunction scm_max_fdsfunction scm_detach_fdsfunction scm_passecfunction scm_has_secdatafunction scm_passecfunction scm_pidfd_recvfunction __scm_recv_commonfunction scm_recvfunction scm_recv_unixexport __scm_destroyexport __scm_sendexport put_cmsgexport put_cmsg_scm_timestamping64export put_cmsg_scm_timestampingexport scm_detach_fdsexport scm_fp_dupexport scm_recv
Annotated Snippet
const struct proto_ops *ops = READ_ONCE(sock->ops);
struct cmsghdr *cmsg;
int err;
for_each_cmsghdr(cmsg, msg) {
err = -EINVAL;
/* Verify that cmsg_len is at least sizeof(struct cmsghdr) */
/* The first check was omitted in <= 2.2.5. The reasoning was
that parser checks cmsg_len in any case, so that
additional check would be work duplication.
But if cmsg_level is not SOL_SOCKET, we do not check
for too short ancillary data object at all! Oops.
OK, let's add it...
*/
if (!CMSG_OK(msg, cmsg))
goto error;
if (cmsg->cmsg_level != SOL_SOCKET)
continue;
switch (cmsg->cmsg_type)
{
case SCM_RIGHTS:
if (!ops || ops->family != PF_UNIX)
goto error;
err=scm_fp_copy(cmsg, &p->fp);
if (err<0)
goto error;
break;
case SCM_CREDENTIALS:
{
struct ucred creds;
kuid_t uid;
kgid_t gid;
if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct ucred)))
goto error;
memcpy(&creds, CMSG_DATA(cmsg), sizeof(struct ucred));
err = scm_check_creds(&creds);
if (err)
goto error;
if (!p->pid || pid_vnr(p->pid) != creds.pid) {
struct pid *pid;
err = -ESRCH;
pid = find_get_pid(creds.pid);
if (!pid)
goto error;
/* pass a struct pid reference from
* find_get_pid() to scm_replace_pid().
*/
err = scm_replace_pid(p, pid);
if (err) {
put_pid(pid);
goto error;
}
}
err = -EINVAL;
uid = make_kuid(current_user_ns(), creds.uid);
gid = make_kgid(current_user_ns(), creds.gid);
if (!uid_valid(uid) || !gid_valid(gid))
goto error;
p->creds.uid = uid;
p->creds.gid = gid;
break;
}
default:
goto error;
}
}
if (p->fp && !p->fp->count)
{
kfree(p->fp);
p->fp = NULL;
}
return 0;
error:
scm_destroy(p);
return err;
}
EXPORT_SYMBOL(__scm_send);
int put_cmsg(struct msghdr * msg, int level, int type, int len, void *data)
{
int cmlen = CMSG_LEN(len);
Annotation
- Immediate include surface: `linux/module.h`, `linux/signal.h`, `linux/capability.h`, `linux/errno.h`, `linux/sched.h`, `linux/sched/user.h`, `linux/mm.h`, `linux/kernel.h`.
- Detected declarations: `function setu`, `function scm_fp_copy`, `function __scm_destroy`, `function scm_replace_pid`, `function __scm_send`, `function for_each_cmsghdr`, `function put_cmsg`, `function scoped_user_write_access_size`, `function put_cmsg_notrunc`, `function put_cmsg_scm_timestamping64`.
- Atlas domain: Networking Core / Sockets, Protocols, Packet Path, And Network Policy.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.