Skip to content
Permalink
Browse files
net: socket: implement SO_DESCRIPTION
This command attaches the zero terminated string in optval to the
socket for troubleshooting purposes. The free string is displayed in the
process fdinfo file for that fd (/proc/<pid>/fdinfo/<fd>).

One intended usage is to allow processes to self-document sockets
for netstat and friends to report

We ignore optlen and constrain the string to a static max size

Signed-off-by: Pascal Bouchareine <kalou@tfz.net>
  • Loading branch information
kalou authored and intel-lab-lkp committed Aug 22, 2020
1 parent ed781c3 commit 35dcbc957b52151274a9e06b2d6c4739b5061622
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
@@ -342,6 +342,7 @@ struct bpf_sk_storage;
* @sk_txtime_deadline_mode: set deadline mode for SO_TXTIME
* @sk_txtime_report_errors: set report errors mode for SO_TXTIME
* @sk_txtime_unused: unused txtime flags
* @sk_description: user supplied with SO_DESCRIPTION
*/
struct sock {
/*
@@ -519,6 +520,9 @@ struct sock {
struct bpf_sk_storage __rcu *sk_bpf_storage;
#endif
struct rcu_head sk_rcu;

#define SK_MAX_DESC_SIZE 256
char *sk_description;
};

enum sk_pacing {
@@ -119,6 +119,8 @@

#define SO_DETACH_REUSEPORT_BPF 68

#define SO_DESCRIPTION 69

#if !defined(__KERNEL__)

#if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
@@ -828,6 +828,49 @@ void sock_set_rcvbuf(struct sock *sk, int val)
}
EXPORT_SYMBOL(sock_set_rcvbuf);

static int sock_set_description(struct sock *sk, char __user *user_desc)
{
char *old, *desc;

desc = strndup_user(user_desc, SK_MAX_DESC_SIZE, GFP_KERNEL_ACCOUNT);
if (IS_ERR(desc))
return PTR_ERR(desc);

lock_sock(sk);
old = sk->sk_description;
sk->sk_description = desc;
release_sock(sk);

kfree(old);

return 0;
}

static int sock_get_description(struct sock *sk, char __user *optval,
int __user *optlen, int len)
{
char desc[SK_MAX_DESC_SIZE];

lock_sock(sk);
if (sk->sk_description) {
/* strndup_user: len(desc + nul) <= SK_MAX_DESC_SIZE */
len = min_t(unsigned int, len,
strlen(sk->sk_description) + 1);
memcpy(desc, sk->sk_description, len);
} else {
len = 0;
}
release_sock(sk);

if (copy_to_user(optval, desc, len))
return -EFAULT;

if (put_user(len, optlen))
return -EFAULT;

return 0;
}

/*
* This is meant for all protocols to use and covers goings on
* at the socket level. Everything here is generic.
@@ -850,6 +893,9 @@ int sock_setsockopt(struct socket *sock, int level, int optname,
if (optname == SO_BINDTODEVICE)
return sock_setbindtodevice(sk, optval, optlen);

if (optname == SO_DESCRIPTION)
return sock_set_description(sk, optval);

if (optlen < sizeof(int))
return -EINVAL;

@@ -1614,6 +1660,9 @@ int sock_getsockopt(struct socket *sock, int level, int optname,
v.val = sk->sk_bound_dev_if;
break;

case SO_DESCRIPTION:
return sock_get_description(sk, optval, optlen, len);

default:
/* We implement the SO_SNDLOWAT etc to not be settable
* (1003.1g 7).
@@ -1792,6 +1841,8 @@ static void __sk_destruct(struct rcu_head *head)
RCU_INIT_POINTER(sk->sk_filter, NULL);
}

kfree(sk->sk_description);

sock_disable_timestamp(sk, SK_FLAGS_TIMESTAMP);

#ifdef CONFIG_BPF_SYSCALL
@@ -1964,6 +2015,8 @@ struct sock *sk_clone_lock(const struct sock *sk, const gfp_t priority)
if (sk_user_data_is_nocopy(newsk))
newsk->sk_user_data = NULL;

newsk->sk_description = NULL;

newsk->sk_err = 0;
newsk->sk_err_soft = 0;
newsk->sk_priority = 0;
@@ -134,6 +134,11 @@ static void sock_show_fdinfo(struct seq_file *m, struct file *f)
{
struct socket *sock = f->private_data;

lock_sock(sock->sk);
if (sock->sk->sk_description)
seq_printf(m, "desc:\t%s\n", sock->sk->sk_description);
release_sock(sock->sk);

if (sock->ops->show_fdinfo)
sock->ops->show_fdinfo(m, sock);
}

0 comments on commit 35dcbc9

Please sign in to comment.