]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
s3-client: Use the passed down memory context for cli_connect_nb() master
authorAndreas Schneider <asn@samba.org>
Wed, 29 Oct 2025 10:16:53 +0000 (11:16 +0100)
committerAnoop C S <anoopcs@samba.org>
Thu, 6 Nov 2025 09:58:52 +0000 (09:58 +0000)
With the patches coming before this patch, we can use main talloc
context to allocate the connections on and make sure we only free them
after we don't need them anymore.

This fixes a lot of memory leaks found by LeakSanitizer. One example is:

==838668==ERROR: LeakSanitizer: detected memory leaks

Indirect leak of 4784 byte(s) in 13 object(s) allocated from:
    #0 0x7fdb1ef21c2b in malloc (/lib64/libasan.so.8+0x121c2b) (BuildId: cbfe49f3b7600c4f194d4c54774c977296e9d98a)
    #1 0x7fdb1ebbb6a0 in __talloc_with_prefix ../../lib/talloc/talloc.c:783
    #2 0x7fdb1ebbcf75 in __talloc ../../lib/talloc/talloc.c:825
    #3 0x7fdb1ebbcf75 in _talloc_named_const ../../lib/talloc/talloc.c:982
    #4 0x7fdb1ebbcf75 in _talloc_zero ../../lib/talloc/talloc.c:2421
    #5 0x7fdb1cfd1b25 in idr_pre_get ../../lib/util/idtree.c:98
    #6 0x7fdb1cfd1b25 in idr_get_new_above_int ../../lib/util/idtree.c:202
    #7 0x7fdb1cfd2c30 in idr_get_new_above ../../lib/util/idtree.c:368
    #8 0x7fdb1de3246f in map_smb2_handle_to_fnum ../../source3/libsmb/cli_smb2_fnum.c:95
    #9 0x7fdb1de3246f in cli_smb2_create_fnum_done ../../source3/libsmb/cli_smb2_fnum.c:438
    #10 0x7fdb1dbaca05 in _tevent_req_notify_callback ../../lib/tevent/tevent_req.c:177
    #11 0x7fdb1dbacc3c in tevent_req_finish ../../lib/tevent/tevent_req.c:234
    #12 0x7fdb1dbacca4 in _tevent_req_done ../../lib/tevent/tevent_req.c:240
    #13 0x7fdb1ed0eb35 in smb2cli_create_done ../../libcli/smb/smb2cli_create.c:483
    #14 0x7fdb1dbaca05 in _tevent_req_notify_callback ../../lib/tevent/tevent_req.c:177
    #15 0x7fdb1dbacc3c in tevent_req_finish ../../lib/tevent/tevent_req.c:234
    #16 0x7fdb1dbacd74 in tevent_req_trigger ../../lib/tevent/tevent_req.c:291
    #17 0x7fdb1dbaad1e in tevent_common_invoke_immediate_handler ../../lib/tevent/tevent_immediate.c:190
    #18 0x7fdb1dbaad5b in tevent_common_loop_immediate ../../lib/tevent/tevent_immediate.c:236
    #19 0x7fdb1dbbe42f in epoll_event_loop_once ../../lib/tevent/tevent_epoll.c:908
    #20 0x7fdb1dbb7787 in std_event_loop_once ../../lib/tevent/tevent_standard.c:110
    #21 0x7fdb1dba7466 in _tevent_loop_once ../../lib/tevent/tevent.c:860
    #22 0x7fdb1dbad082 in tevent_req_poll ../../lib/tevent/tevent_req.c:342
    #23 0x7fdb1eaa93d4 in tevent_req_poll_ntstatus ../../lib/util/tevent_ntstatus.c:109
    #24 0x7fdb1de138bb in cli_list ../../source3/libsmb/clilist.c:1188
    #25 0x000000239f0f in do_list ../../source3/client/client.c:853
    #26 0x00000023a93a in cmd_dir ../../source3/client/client.c:936
    #27 0x00000023f090 in process_stdin ../../source3/client/client.c:6215
    #28 0x00000023f090 in process ../../source3/client/client.c:6269
    #29 0x00000023f090 in main ../../source3/client/client.c:6811
    #30 0x7fdb1ac2b2fa in __libc_start_call_main (/lib64/libc.so.6+0x2b2fa) (BuildId: 8523b213e7586a93ab00f6dd476418b1e521e62c)
    #31 0x7ffd7d5613af  ([stack]+0x3a3af)

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Anoop C S <anoopcs@samba.org>
Autobuild-User(master): Anoop C S <anoopcs@samba.org>
Autobuild-Date(master): Thu Nov  6 09:58:52 UTC 2025 on atb-devel-224

source3/libsmb/clidfs.c

index cb0d8a88eccdcd65059a338e8c88c1da7c002028..b0f37020ef7cfe824e0e4ab7edc693f614abbb4f 100644 (file)
@@ -181,9 +181,10 @@ static NTSTATUS do_connect(TALLOC_CTX *ctx,
        /*
         * The functions cli_resolve_path() and cli_cm_open() might not create a
         * new cli context, but might return an already existing one. This
-        * forces us to have a long lived cli allocated on the NULL context.
+        * requires that a long living context is used. It should be freed after
+        * the client is done with its connection.
         */
-       status = cli_connect_nb(NULL,
+       status = cli_connect_nb(ctx,
                                server,
                                dest_ss,
                                transports,
@@ -425,10 +426,55 @@ static struct cli_state *cli_cm_find(struct cli_state *cli,
        return NULL;
 }
 
-/****************************************************************************
- Open a client connection to a \\server\share.
-****************************************************************************/
-
+/**
+ * @brief Open a client connection to a \\server\share.
+ *
+ * This function attempts to establish a connection to a specified SMB server
+ * and share. It first checks for an existing connection in the referring_cli
+ * connection list to avoid creating duplicate connections. If no existing
+ * connection is found, it creates a new one.
+ *
+ * @param[in] ctx           The memory context. This context needs to be a
+ *                          long-living context, as cli_resolve_path() and
+ *                          cli_cm_open() might not create a new cli context,
+ *                          but might return an existing one.
+ *                          The context should be freed after the client is
+ *                          fully done with its connections.
+ *
+ * @param[in] referring_cli An existing cli_state to search for cached
+ *                          connections. This is used to find previously
+ *                          established connections to avoid creating duplicates.
+ *                          Can be NULL if no existing connections exist.
+ *
+ * @param[in] server        The server name to connect to. This is the NetBIOS
+ *                          name or hostname of the target server.
+ *
+ * @param[in] share         The share name to connect to (can also be "IPC$").
+ *
+ * @param[in] creds         Credentials for authentication. Must not be NULL.
+ *                          The function will return NT_STATUS_INVALID_PARAMETER
+ *                          if credentials are not provided.
+ *
+ * @param[in] dest_ss       Destination socket address. Can be NULL, in which
+ *                          case the address will be resolved from the server
+ *                          name using name resolution.
+ *
+ * @param[in] transports    SMB transport configuration specifying which SMB
+ *                          protocol versions to use (e.g., SMB1, SMB2, SMB3).
+ *
+ * @param[in] name_type     NetBIOS name type for name resolution. Common values
+ *                          include 0x00 (workstation), 0x20 (file server).
+ *                          This is used when resolving the server name via
+ *                          NetBIOS.
+ *
+ * @param[out] pcli         Pointer to receive the cli_state structure. On
+ *                          success, this will point to either a newly created
+ *                          connection or an existing cached connection.
+ *
+ * @return                  NT_STATUS_OK on success. NT_STATUS_INVALID_PARAMETER
+ *                          if credentials are NULL. Other NTSTATUS error codes
+ *                          on connection or authentication failures.
+ */
 NTSTATUS cli_cm_open(TALLOC_CTX *ctx,
                     struct cli_state *referring_cli,
                     const char *server,