From f8c677a04180677b79fac80deba0268ceae5a2dc Mon Sep 17 00:00:00 2001 From: shuqiang-zheng Date: Fri, 9 Aug 2024 17:09:31 +0800 Subject: [PATCH] fix(security): slice memory allocation with excessive size value. Signed-off-by: shuqiang-zheng --- authnode/authnode_manager.go | 2 +- authnode/config.go | 6 +++--- authnode/server.go | 8 ++++---- cli/cmd/vol.go | 12 ++++++------ datanode/server.go | 2 +- datanode/server_handler.go | 6 +++--- metanode/dentry.go | 21 +++++++++++++++++++++ metanode/inode.go | 21 +++++++++++++++++++++ metanode/meta_quota_manager.go | 6 ++++++ proto/transaction.go | 1 + 10 files changed, 67 insertions(+), 18 deletions(-) diff --git a/authnode/authnode_manager.go b/authnode/authnode_manager.go index 1dc332e47..f98cf5626 100644 --- a/authnode/authnode_manager.go +++ b/authnode/authnode_manager.go @@ -57,7 +57,7 @@ func (m *Server) handlePeerChange(confChange *proto.ConfChange) (err error) { msg = fmt.Sprintf("action[handlePeerChange] clusterID[%v] nodeAddr[%v] is invalid", m.clusterName, addr) break } - m.raftStore.AddNodeWithPort(confChange.Peer.ID, arr[0], int(m.config.heartbeatPort), int(m.config.replicaPort)) + m.raftStore.AddNodeWithPort(confChange.Peer.ID, arr[0], m.config.heartbeatPort, m.config.replicaPort) AddrDatabase[confChange.Peer.ID] = string(confChange.Context) msg = fmt.Sprintf("clusterID[%v] peerID:%v,nodeAddr[%v] has been add", m.clusterName, confChange.Peer.ID, addr) case proto.ConfRemoveNode: diff --git a/authnode/config.go b/authnode/config.go index 2aaeeaf4c..05ffbc403 100644 --- a/authnode/config.go +++ b/authnode/config.go @@ -42,8 +42,8 @@ const ( type clusterConfig struct { peers []raftstore.PeerAddress peerAddrs []string - heartbeatPort int64 - replicaPort int64 + heartbeatPort int + replicaPort int } // AddrDatabase is a map that stores the address of a given host (e.g., the leader) @@ -76,7 +76,7 @@ func (cfg *clusterConfig) parsePeers(peerStr string) error { if err != nil { return err } - cfg.peers = append(cfg.peers, raftstore.PeerAddress{Peer: proto.Peer{ID: id}, Address: ip, HeartbeatPort: int(cfg.heartbeatPort), ReplicaPort: int(cfg.replicaPort)}) + cfg.peers = append(cfg.peers, raftstore.PeerAddress{Peer: proto.Peer{ID: id}, Address: ip, HeartbeatPort: cfg.heartbeatPort, ReplicaPort: cfg.replicaPort}) address := fmt.Sprintf("%v:%v", ip, port) syslog.Println(address) AddrDatabase[id] = address diff --git a/authnode/server.go b/authnode/server.go index fb939ca22..c43b8b412 100644 --- a/authnode/server.go +++ b/authnode/server.go @@ -109,8 +109,8 @@ func (m *Server) checkConfig(cfg *config.Config) (err error) { if m.id, err = strconv.ParseUint(cfg.GetString(ID), 10, 64); err != nil { return fmt.Errorf("%v,err:%v", proto.ErrInvalidCfg, err.Error()) } - m.config.heartbeatPort = cfg.GetInt64(heartbeatPortKey) - m.config.replicaPort = cfg.GetInt64(replicaPortKey) + m.config.heartbeatPort = cfg.GetInt(heartbeatPortKey) + m.config.replicaPort = cfg.GetInt(replicaPortKey) if m.config.heartbeatPort <= 1024 { m.config.heartbeatPort = raftstore.DefaultHeartbeatPort } @@ -162,8 +162,8 @@ func (m *Server) createRaftServer(cfg *config.Config) (err error) { NodeID: m.id, RaftPath: m.walDir, NumOfLogsToRetain: m.retainLogs, - HeartbeatPort: int(m.config.heartbeatPort), - ReplicaPort: int(m.config.replicaPort), + HeartbeatPort: m.config.heartbeatPort, + ReplicaPort: m.config.replicaPort, TickInterval: m.tickInterval, ElectionTick: m.electionTick, } diff --git a/cli/cmd/vol.go b/cli/cmd/vol.go index aeefee842..2d8da25aa 100644 --- a/cli/cmd/vol.go +++ b/cli/cmd/vol.go @@ -902,15 +902,15 @@ func newVolAddDPCmd(client *master.MasterClient) *cobra.Command { defer func() { errout(err) }() - var count int64 - if count, err = strconv.ParseInt(number, 10, 64); err != nil { + var count int + if count, err = strconv.Atoi(number); err != nil { return } if count < 1 { err = fmt.Errorf("number must be larger than 0") return } - if err = client.AdminAPI().CreateDataPartition(volume, int(count), clientIDKey); err != nil { + if err = client.AdminAPI().CreateDataPartition(volume, count, clientIDKey); err != nil { return } stdout("Add dp successfully.\n") @@ -944,15 +944,15 @@ func newVolAddMPCmd(client *master.MasterClient) *cobra.Command { defer func() { errout(err) }() - var count int64 - if count, err = strconv.ParseInt(number, 10, 64); err != nil { + var count int + if count, err = strconv.Atoi(number); err != nil { return } if count < 1 { err = fmt.Errorf("number must be larger than 0") return } - if err = client.AdminAPI().CreateMetaPartition(volume, int(count), clientIDKey); err != nil { + if err = client.AdminAPI().CreateMetaPartition(volume, count, clientIDKey); err != nil { return } stdout("Add mp successfully.\n") diff --git a/datanode/server.go b/datanode/server.go index 3e3020da3..e9119221a 100644 --- a/datanode/server.go +++ b/datanode/server.go @@ -835,7 +835,7 @@ func (s *DataNode) serveSmuxStream(stream *smux.Stream) { func (s *DataNode) parseSmuxConfig(cfg *config.Config) error { s.enableSmuxConnPool = cfg.GetBool(ConfigKeyEnableSmuxClient) - s.smuxPortShift = int(cfg.GetInt64(ConfigKeySmuxPortShift)) + s.smuxPortShift = cfg.GetInt(ConfigKeySmuxPortShift) if s.smuxPortShift == 0 { s.smuxPortShift = util.DefaultSmuxPortShift } diff --git a/datanode/server_handler.go b/datanode/server_handler.go index 21cbee883..eb06805a5 100644 --- a/datanode/server_handler.go +++ b/datanode/server_handler.go @@ -306,13 +306,13 @@ func (s *DataNode) setDiskQos(w http.ResponseWriter, r *http.Request) { s.buildFailureResp(w, http.StatusBadRequest, err.Error()) return } - parser := func(key string) (val int64, err error, has bool) { + parser := func(key string) (val int, err error, has bool) { valStr := r.FormValue(key) if valStr == "" { return 0, nil, false } has = true - val, err = strconv.ParseInt(valStr, 10, 64) + val, err = strconv.Atoi(valStr) return } @@ -332,7 +332,7 @@ func (s *DataNode) setDiskQos(w http.ResponseWriter, r *http.Request) { } if has { updated = true - *pVal = int(val) + *pVal = val } } diff --git a/metanode/dentry.go b/metanode/dentry.go index 33cb0a7f2..7006dd946 100644 --- a/metanode/dentry.go +++ b/metanode/dentry.go @@ -428,6 +428,9 @@ func (td *TxDentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data := make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -442,6 +445,9 @@ func (td *TxDentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data = make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -513,6 +519,9 @@ func (td *TxUpdateDentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data := make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -527,6 +536,9 @@ func (td *TxUpdateDentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data = make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -541,6 +553,9 @@ func (td *TxUpdateDentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data = make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -592,6 +607,9 @@ func (d *Dentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &keyLen); err != nil { return } + if keyLen > proto.MaxBufferSize { + keyLen = proto.MaxBufferSize + } keyBytes := make([]byte, keyLen) if _, err = buff.Read(keyBytes); err != nil { return @@ -602,6 +620,9 @@ func (d *Dentry) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &valLen); err != nil { return } + if valLen > proto.MaxBufferSize { + valLen = proto.MaxBufferSize + } valBytes := make([]byte, valLen) if _, err = buff.Read(valBytes); err != nil { return diff --git a/metanode/inode.go b/metanode/inode.go index 84b635016..a6b6bf097 100644 --- a/metanode/inode.go +++ b/metanode/inode.go @@ -309,6 +309,9 @@ func (ti *TxInode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data := make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -322,6 +325,9 @@ func (ti *TxInode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &dataLen); err != nil { return } + if dataLen > proto.MaxBufferSize { + dataLen = proto.MaxBufferSize + } data = make([]byte, int(dataLen)) if _, err = buff.Read(data); err != nil { return @@ -542,6 +548,9 @@ func (i *Inode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &keyLen); err != nil { return } + if keyLen > proto.MaxBufferSize { + keyLen = proto.MaxBufferSize + } keyBytes := make([]byte, keyLen) if _, err = buff.Read(keyBytes); err != nil { return @@ -552,6 +561,9 @@ func (i *Inode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &valLen); err != nil { return } + if valLen > proto.MaxBufferSize { + valLen = proto.MaxBufferSize + } valBytes := make([]byte, valLen) if _, err = buff.Read(valBytes); err != nil { return @@ -765,6 +777,9 @@ func (i *Inode) UnmarshalInodeValue(buff *bytes.Buffer) (err error) { return } if symSize > 0 { + if symSize > proto.MaxBufferSize { + symSize = proto.MaxBufferSize + } i.LinkTarget = make([]byte, symSize) if _, err = io.ReadFull(buff, i.LinkTarget); err != nil { return @@ -798,6 +813,9 @@ func (i *Inode) UnmarshalInodeValue(buff *bytes.Buffer) (err error) { return } if extSize > 0 { + if extSize > proto.MaxBufferSize { + extSize = proto.MaxBufferSize + } extBytes := make([]byte, extSize) if _, err = io.ReadFull(buff, extBytes); err != nil { return @@ -829,6 +847,9 @@ func (i *Inode) UnmarshalInodeValue(buff *bytes.Buffer) (err error) { return } if ObjExtSize > 0 { + if ObjExtSize > proto.MaxBufferSize { + ObjExtSize = proto.MaxBufferSize + } objExtBytes := make([]byte, ObjExtSize) if _, err = io.ReadFull(buff, objExtBytes); err != nil { return diff --git a/metanode/meta_quota_manager.go b/metanode/meta_quota_manager.go index 018d8f182..6ff55b85a 100644 --- a/metanode/meta_quota_manager.go +++ b/metanode/meta_quota_manager.go @@ -90,6 +90,9 @@ func (qInode *MetaQuotaInode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &inodeLen); err != nil { return } + if inodeLen > proto.MaxBufferSize { + inodeLen = proto.MaxBufferSize + } inodeBytes := make([]byte, inodeLen) if _, err = buff.Read(inodeBytes); err != nil { return @@ -142,6 +145,9 @@ func (qInode *TxMetaQuotaInode) Unmarshal(raw []byte) (err error) { if err = binary.Read(buff, binary.BigEndian, &inodeLen); err != nil { return } + if inodeLen > proto.MaxBufferSize { + inodeLen = proto.MaxBufferSize + } inodeBytes := make([]byte, inodeLen) if _, err = buff.Read(inodeBytes); err != nil { return diff --git a/proto/transaction.go b/proto/transaction.go index b6ee78163..8425be81d 100644 --- a/proto/transaction.go +++ b/proto/transaction.go @@ -38,6 +38,7 @@ const ( MinTxConflictRetryInterval = 10 // ms DefaultTxDeleteTime = 120 ClearOrphanTxTime = 3600 + MaxBufferSize = 1024 * 1024 * 1024 // 1GB ) type TxOpMask uint8