Harden the authorizer access to prevent crashes

This commit is contained in:
Bohdan Horbeshko 2023-08-31 17:26:35 -04:00
parent aa561c5be6
commit 4588170d1e
5 changed files with 35 additions and 2 deletions

View file

@ -2,7 +2,7 @@
COMMIT := $(shell git rev-parse --short HEAD)
TD_COMMIT := "8517026415e75a8eec567774072cbbbbb52376c1"
VERSION := "v1.8.0"
VERSION := "v1.8.1"
MAKEOPTS := "-j4"
all:

View file

@ -15,7 +15,7 @@ import (
goxmpp "gosrc.io/xmpp"
)
var version string = "1.8.0"
var version string = "1.8.1"
var commit string
var sm *goxmpp.StreamManager

View file

@ -74,6 +74,9 @@ type clientLocks struct {
resourcesLock sync.Mutex
outboxLock sync.Mutex
lastMsgHashesLock sync.Mutex
authorizerReadLock sync.Mutex
authorizerWriteLock sync.Mutex
}
// NewClient instantiates a Telegram App

View file

@ -244,6 +244,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
return notEnoughArguments
}
c.locks.authorizerWriteLock.Lock()
defer c.locks.authorizerWriteLock.Unlock()
if cmd == "login" {
err := c.TryLogin(resource, args[0])
if err != nil {
@ -324,10 +327,13 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
lastname = rawCmdArguments(cmdline, 1)
}
c.locks.authorizerWriteLock.Lock()
if c.authorizer != nil && !c.authorizer.isClosed {
c.authorizer.FirstName <- firstname
c.authorizer.LastName <- lastname
c.locks.authorizerWriteLock.Unlock()
} else {
c.locks.authorizerWriteLock.Unlock()
if !c.Online() {
return notOnline
}

View file

@ -110,6 +110,7 @@ func (c *Client) Connect(resource string) error {
log.Warn("Connecting to Telegram network...")
c.locks.authorizerWriteLock.Lock()
c.authorizer = &clientAuthorizer{
TdlibParameters: make(chan *client.SetTdlibParametersRequest, 1),
PhoneNumber: make(chan string, 1),
@ -123,6 +124,7 @@ func (c *Client) Connect(resource string) error {
go c.interactor()
c.authorizer.TdlibParameters <- c.parameters
c.locks.authorizerWriteLock.Unlock()
tdlibClient, err := client.NewClient(c.authorizer, c.options...)
if err != nil {
@ -178,6 +180,9 @@ func (c *Client) TryLogin(resource string, login string) error {
time.Sleep(1e5)
}
c.locks.authorizerReadLock.Lock()
defer c.locks.authorizerReadLock.Unlock()
if c.authorizer == nil {
return errors.New(TelegramNotInitialized)
}
@ -190,6 +195,9 @@ func (c *Client) TryLogin(resource string, login string) error {
}
func (c *Client) SetPhoneNumber(login string) error {
c.locks.authorizerWriteLock.Lock()
defer c.locks.authorizerWriteLock.Unlock()
if c.authorizer == nil || c.authorizer.isClosed {
return errors.New("Authorization not needed")
}
@ -234,9 +242,16 @@ func (c *Client) Disconnect(resource string, quit bool) bool {
func (c *Client) interactor() {
for {
c.locks.authorizerReadLock.Lock()
if c.authorizer == nil {
log.Warn("Authorizer is lost, halting the interactor")
c.locks.authorizerReadLock.Unlock()
return
}
state, ok := <-c.authorizer.State
if !ok {
log.Warn("Interactor is disconnected")
c.locks.authorizerReadLock.Unlock()
return
}
@ -266,18 +281,27 @@ func (c *Client) interactor() {
log.Warn("Waiting for 2FA password...")
gateway.SendServiceMessage(c.jid, "Please, enter 2FA passphrase via /password 12345", c.xmpp)
}
c.locks.authorizerReadLock.Unlock()
}
}
func (c *Client) forceClose() {
c.locks.authorizerReadLock.Lock()
c.locks.authorizerWriteLock.Lock()
defer c.locks.authorizerReadLock.Unlock()
defer c.locks.authorizerWriteLock.Unlock()
c.online = false
c.authorizer = nil
}
func (c *Client) close() {
c.locks.authorizerWriteLock.Lock()
if c.authorizer != nil && !c.authorizer.isClosed {
c.authorizer.Close()
}
c.locks.authorizerWriteLock.Unlock()
if c.client != nil {
_, err := c.client.Close()
if err != nil {