The branch main has been updated by kp:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=6a23843a4a6f2dae95a2029358468b697072b8d2

commit 6a23843a4a6f2dae95a2029358468b697072b8d2
Author:     Kristof Provost <k...@freebsd.org>
AuthorDate: 2023-04-28 16:18:55 +0000
Commit:     Kristof Provost <k...@freebsd.org>
CommitDate: 2023-04-30 18:11:54 +0000

    libifconfig: fix carp key configuration
    
    There were two issues with the carp key configuration in the new netlink
    code.
    
    The first is that userspace failed to actually pass the CARP_NL_KEY
    attribute to the kernel, so a key was never set.
    
    The second issue is that snl_attr_get_string() returns a pointer to the
    string inside the netlink message. It does not copy the string to the
    target buffer. That's somewhat inconvenient to work with in libifconfig
    where we have a static buffer for the key.
    Introduce snl_attr_copy_string() which can copy a string to a target
    buffer and uses the 'arg' parameter to pass the buffer size, so it
    doesn't accidentally exceed the available space.
    
    Reviewed by:    melifaro
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D39874
---
 lib/libifconfig/libifconfig_carp.c |  3 ++-
 sys/netlink/netlink_snl.h          | 20 +++++++++++++++++++-
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/lib/libifconfig/libifconfig_carp.c 
b/lib/libifconfig/libifconfig_carp.c
index 2b612f0ba51b..cb1a998b3a0c 100644
--- a/lib/libifconfig/libifconfig_carp.c
+++ b/lib/libifconfig/libifconfig_carp.c
@@ -55,7 +55,7 @@ static struct snl_attr_parser ap_carp_get[] = {
        { .type = CARP_NL_STATE, .off = _OUT(carpr_state), .cb = 
snl_attr_get_uint32 },
        { .type = CARP_NL_ADVBASE, .off = _OUT(carpr_advbase), .cb = 
snl_attr_get_int32 },
        { .type = CARP_NL_ADVSKEW, .off = _OUT(carpr_advskew), .cb = 
snl_attr_get_int32 },
-       { .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = 
snl_attr_get_string },
+       { .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = 
snl_attr_copy_string, .arg_u32 = CARP_KEY_LEN },
        { .type = CARP_NL_ADDR, .off = _OUT(carpr_addr), .cb = 
snl_attr_get_in_addr },
        { .type = CARP_NL_ADDR6, .off = _OUT(carpr_addr6), .cb = 
snl_attr_get_in6_addr },
 };
@@ -176,6 +176,7 @@ ifconfig_carp_set_info(ifconfig_handle_t *h, const char 
*name,
            &carpr->carpr_addr);
        snl_add_msg_attr(&nw, CARP_NL_ADDR6, sizeof(carpr->carpr_addr6),
            &carpr->carpr_addr6);
+       snl_add_msg_attr_string(&nw, CARP_NL_KEY, carpr->carpr_key);
 
        hdr = snl_finalize_msg(&nw);
        if (hdr == NULL) {
diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h
index 4cb1b3e13abc..822cec9b4dc4 100644
--- a/sys/netlink/netlink_snl.h
+++ b/sys/netlink/netlink_snl.h
@@ -138,7 +138,12 @@ struct snl_attr_parser {
        uint16_t                type;   /* Attribute type */
        uint16_t                off;    /* field offset in the target structure 
*/
        snl_parse_attr_f        *cb;    /* parser function to call */
-       const void              *arg;   /* Optional argument parser */
+
+       /* Optional parser argument */
+       union {
+               const void              *arg;
+               const uint32_t           arg_u32;
+       };
 };
 
 struct snl_hdr_parser {
@@ -588,6 +593,19 @@ snl_attr_get_stringn(struct snl_state *ss, struct nlattr 
*nla,
        return (true);
 }
 
+static inline bool
+snl_attr_copy_string(struct snl_state *ss, struct nlattr *nla,
+    const void *arg, void *target)
+{
+       char *tmp;
+
+       if (snl_attr_get_string(ss, nla, NULL, &tmp)) {
+               strlcpy(target, tmp, (size_t)arg);
+               return (true);
+       }
+       return (false);
+}
+
 static inline bool
 snl_attr_get_nested(struct snl_state *ss, struct nlattr *nla, const void *arg, 
void *target)
 {

Reply via email to