Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp1180769ybi; Thu, 30 May 2019 12:58:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqyG5s+/W0DBb7PwXhxbM/kFezQkGyq+rNsBXPNLDIMQjO0kO7ggI1YkyRUm/AH8cjZ4SMzo X-Received: by 2002:a65:6256:: with SMTP id q22mr5272341pgv.190.1559246326811; Thu, 30 May 2019 12:58:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559246326; cv=none; d=google.com; s=arc-20160816; b=twEFam0f1D5bQWcb6cedVbkrDPbtrqgq8tws5w+9SUCn6zLKbbozqBGBajI17eVaWe VoAuekWVjowWTxcMRSVgSx0uNHKy1fFJe5bwiUJEpmk2Tc6qypoZEte4k/dGuHc77YSZ BAaoEeccJygO2aRxsQ/3KNGTUlQayjRboptITm6A8vPWOLYhmvRo7rMOgtDv1dXCthz9 m/86w93FVn4FkE/Q1QSfOqURgsqB+UnxjQusg5ftnA3FtWq5Ogge8RsA37V+j/R2+tVc iFTTnISUTCyU2pylMp+HiN2ev8KminwYVpayBKqX4l+3Wq7y1HE6jKFcctisBLbXvxGY SnGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=zIublxtIOHl0RENs9cezg0tytpoQ6p49L/c3FdiIrPc=; b=ZxJW71MdhtdwIoktnmsDbIlZcZOmk1HvSgBFxmlgULBKSBhs7xIwuaJVWL7KGSqZUe SdRN6xYTakDzNeXWEAeoCJ84hPJZVBggNtDPp39OtAOB8VGHQY3RGoJZMJZLCs1rRFi2 GlCUC82zWhdQYXuKABTvvLcWhGOdVCXVLrucFXH+fpXtuIax9QDwV8r52xQhHmg7evVP Jc8gURcBypBxHt3yPNRH8pm+fRrCKSkQL3HdCMKpcLvDg89RIwCnCYVwwNmzTd8RA93I BfLKEJwUXkEZIHBKHATpX1lNu5zt6m0rjNHTXYm8RHflSDzBhqy7LqhJ2Z5adneX99xz Nscw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c134si3708690pga.268.2019.05.30.12.58.30; Thu, 30 May 2019 12:58:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726593AbfE3T5M (ORCPT + 99 others); Thu, 30 May 2019 15:57:12 -0400 Received: from charlotte.tuxdriver.com ([70.61.120.58]:41370 "EHLO smtp.tuxdriver.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725897AbfE3T5L (ORCPT ); Thu, 30 May 2019 15:57:11 -0400 Received: from cpe-2606-a000-111b-405a-0-0-0-162e.dyn6.twc.com ([2606:a000:111b:405a::162e] helo=localhost) by smtp.tuxdriver.com with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.63) (envelope-from ) id 1hWRAc-0002cj-39; Thu, 30 May 2019 15:57:04 -0400 Date: Thu, 30 May 2019 15:56:34 -0400 From: Neil Horman To: Marcelo Ricardo Leitner Cc: syzbot , davem@davemloft.net, linux-kernel@vger.kernel.org, linux-sctp@vger.kernel.org, netdev@vger.kernel.org, syzkaller-bugs@googlegroups.com, vyasevich@gmail.com Subject: Re: memory leak in sctp_process_init Message-ID: <20190530195634.GD1966@hmswarspite.think-freely.org> References: <00000000000097abb90589e804fd@google.com> <20190528013600.GM5506@localhost.localdomain> <20190528111550.GA4658@hmswarspite.think-freely.org> <20190529190709.GE31099@hmswarspite.think-freely.org> <20190529233757.GC3713@localhost.localdomain> <20190530142011.GC1966@hmswarspite.think-freely.org> <20190530151705.GD3713@localhost.localdomain> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190530151705.GD3713@localhost.localdomain> User-Agent: Mutt/1.11.3 (2019-02-01) X-Spam-Score: -2.9 (--) X-Spam-Status: No Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 30, 2019 at 12:17:05PM -0300, Marcelo Ricardo Leitner wrote: > On Thu, May 30, 2019 at 10:20:11AM -0400, Neil Horman wrote: > > On Wed, May 29, 2019 at 08:37:57PM -0300, Marcelo Ricardo Leitner wrote: > > > On Wed, May 29, 2019 at 03:07:09PM -0400, Neil Horman wrote: > > > > --- a/net/sctp/sm_make_chunk.c > > > > +++ b/net/sctp/sm_make_chunk.c > > > > @@ -2419,9 +2419,12 @@ int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk, > > > > /* Copy cookie in case we need to resend COOKIE-ECHO. */ > > > > cookie = asoc->peer.cookie; > > > > if (cookie) { > > > > + if (asoc->peer.cookie_allocated) > > > > + kfree(cookie); > > > > asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp); > > > > if (!asoc->peer.cookie) > > > > goto clean_up; > > > > + asoc->peer.cookie_allocated=1; > > > > } > > > > > > > > /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily > > > > > > What if we kmemdup directly at sctp_process_param(), as it's done for > > > others already? Like SCTP_PARAM_RANDOM and SCTP_PARAM_HMAC_ALGO. I > > > don't see a reason for SCTP_PARAM_STATE_COOKIE to be different > > > here. This way it would be always allocated, and ready to be kfreed. > > > > > > We still need to free it after the handshake, btw. > > > > > > Marcelo > > > > > > > Still untested, but something like this? > > > > Yes, just.. > > > > > diff --git a/net/sctp/associola.c b/net/sctp/associola.c > > index d2c7d0d2abc1..718b9917844e 100644 > > --- a/net/sctp/associola.c > > +++ b/net/sctp/associola.c > > @@ -393,6 +393,7 @@ void sctp_association_free(struct sctp_association *asoc) > > kfree(asoc->peer.peer_random); > > kfree(asoc->peer.peer_chunks); > > kfree(asoc->peer.peer_hmacs); > > + kfree(asoc->peer.cookie); > > this chunk is not needed because it is freed right above the first > kfree() in the context here. > Ah, thanks, missed that. > > > > /* Release the transport structures. */ > > list_for_each_safe(pos, temp, &asoc->peer.transport_addr_list) { > > diff --git a/net/sctp/sm_make_chunk.c b/net/sctp/sm_make_chunk.c > > index 72e74503f9fc..ff365f22a3c1 100644 > > --- a/net/sctp/sm_make_chunk.c > > +++ b/net/sctp/sm_make_chunk.c > > @@ -2431,14 +2431,6 @@ int sctp_process_init(struct sctp_association *asoc, struct sctp_chunk *chunk, > > /* Peer Rwnd : Current calculated value of the peer's rwnd. */ > > asoc->peer.rwnd = asoc->peer.i.a_rwnd; > > > > - /* Copy cookie in case we need to resend COOKIE-ECHO. */ > > - cookie = asoc->peer.cookie; > > - if (cookie) { > > - asoc->peer.cookie = kmemdup(cookie, asoc->peer.cookie_len, gfp); > > - if (!asoc->peer.cookie) > > - goto clean_up; > > - } > > - > > /* RFC 2960 7.2.1 The initial value of ssthresh MAY be arbitrarily > > * high (for example, implementations MAY use the size of the receiver > > * advertised window). > > @@ -2607,7 +2599,9 @@ static int sctp_process_param(struct sctp_association *asoc, > > case SCTP_PARAM_STATE_COOKIE: > > asoc->peer.cookie_len = > > ntohs(param.p->length) - sizeof(struct sctp_paramhdr); > > - asoc->peer.cookie = param.cookie->body; > > + asoc->peer.cookie = kmemdup(param.cookie->body, asoc->peer.cookie_len, gfp); > > + if (!asoc->peer.cookie) > > + retval = 0; > > break; > > > > case SCTP_PARAM_HEARTBEAT_INFO: > > Plus: > > --- a/net/sctp/sm_sideeffect.c > +++ b/net/sctp/sm_sideeffect.c > @@ -898,6 +898,11 @@ static void sctp_cmd_new_state(struct sctp_cmd_seq *cmds, > asoc->rto_initial; > } > > + if (sctp_state(asoc, ESTABLISHED)) { > + kfree(asoc->peer.cookie); > + asoc->peer.cookie = NULL; > + } > + Not sure I follow why this is needed. It doesn't hurt anything of course, but if we're freeing in sctp_association_free, we don't need to duplicate the operation here, do we? > if (sctp_state(asoc, ESTABLISHED) || > sctp_state(asoc, CLOSED) || > sctp_state(asoc, SHUTDOWN_RECEIVED)) { > > Also untested, just sharing the idea. > > Marcelo >